獲取rootViewController 之window

你一般怎么獲取工程中window上面的RootViewController

第一種方法:

UIWindow *window = [UIApplication sharedApplication].keyWindow;

UIViewController *rootViewController = window.rootViewController;

第二種方法:

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

UIViewController *rootViewController1 = appdelegate.window.rootViewController;

這兩種寫法,在平常的時候是沒有區別的,但是這兩種寫法在有的時候就不一樣了。

keyWindow這個屬性是什么意思?

Paste_Image.png

個人理解的意思是,在windows數組中,最近時間調用makeKeyAndVisible方法的屬性。

當然可以參考關于UIAlertView顯示的相關內容:

http://www.lxweimin.com/p/7ac398ef4532

http://stackoverflow.com/questions/5968703/how-to-find-root-uiviewcontroller

alertView的出現是因為,生成了一個新的window,加在了界面上面。這個時候獲取到的keyWindow就是UIAlertControllerShimPresenterWindow。可以通過如下代碼實驗:

Appdelegate中的代碼,設置RootViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

self.window.backgroundColor = [UIColor whiteColor];

ViewController *view = [[ViewController alloc] init];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];

self.window.rootViewController = nav;

[self.window makeKeyAndVisible];

return YES;

}

ViewController中的代碼

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeSystem];

tempBtn.frame = CGRectMake(100, 100, 100, 100);

tempBtn.backgroundColor = [UIColor cyanColor];

[tempBtn setTitle:@"測試1" forState:UIControlStateNormal];

[tempBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:tempBtn];

}

- (void)clickBtn:(UIButton *)sender{

// 創建一個測試的alertView

UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"測試" message:@"測試" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

[alterView show];

UIWindow *window1 = [UIApplication sharedApplication].keyWindow;

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

UIWindow *window2 = appdelegate.window;

NSLog(@"\n\nwindow1 = %@? ? \n\nwindow2 = %@? \n\nwindow1.rootViewController = %@ \n\nwindow2.rootViewController = %@",window1,window2,window1.rootViewController,window2.rootViewController);

}

打印結果:

window1 = <_UIAlertControllerShimPresenterWindow: 0x7fb4ab720c80; frame = (0 0; 375 667); opaque = NO; gestureRecognizers = ; layer = >

window2 = ; layer = >

window1.rootViewController =

window2.rootViewController =

結果明顯不一樣,其實我們一般情況下想獲取的rootViewController是第二種,希望我們獲取到在appdelegate中設置的appdelaget.window.rootViewController。

所以了建議獲取rootViewController的時候還是采用

第二種方法:

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

UIViewController *rootViewController1 = appdelegate.window.rootViewController;

其實,和alertView類似的,UIActionSheet也是這樣的。

一般人說無所謂,但是如果在AlertView彈出的時候去獲取RootViewController,并且對你認為獲取正確的RootViewController做相關的操作,你會死的很慘。

還有就是建議:即時通過第二種方法獲取到了RootViewController,在使用之前建議再判斷一下獲取到的類是不是就是自己想要的類型,更保險一些。

AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

if ([appdelegate.window.rootViewController isKindOfClass:["想要獲取到的rootVC" class]] == YES) {

// 為所欲為

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容