獲取工程中window的rootViewcontroller的方法:
- 方法一
UIWindow *window = [UIApplication shareApplication].keyWindow;
UIViewController *rootViewController = window.rootViewController;
*方法二
AppDelegate *appDelegate = (AppDelegate*)[UIApplication shareApplication].delegate;
UIViewController *rootViewController = appDelegate.window.rootViewController;
兩種方法獲取到的在一般的時(shí)候是沒有區(qū)別的夺克,但是在有時(shí)候就不一樣了嘀粱。
[UIApplication sharedApplication].keyWindow
其中的keyWindow:我理解的是在windows數(shù)組中最近調(diào)用makeKeyAndVisible方法的屬性提针。
這個(gè)我們可以參考UIAlertView來理解:
alertView的出現(xiàn)是生成了一個(gè)新的window攒暇,展示在界面上海雪。這時(shí)我們獲取到的keyWindow就是UIAlertControllerShimPresenterWindow這時(shí)我們通過keyWindow獲取到的rootViewcontroller就不對(duì)了锦爵。所以我建議還是通過第二種方法來獲取rootViewcontroller