Basheer_CAD的答案是不正確的。在iOS中它們並不總是一樣的。
傑弗瑞托馬斯的答案是正確的,讓我提供一個具體的例子。
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"keyWindow --------> %@",[UIApplication sharedApplication].keyWindow.rootViewController);
NSLog(@"delegate.window --> %@",[UIApplication sharedApplication].delegate.window.rootViewController);
NSLog(@"self.view.window -> %@",self.view.window.rootViewController);
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alert show];
NSLog(@"keyWindow --------> %@",[UIApplication sharedApplication].keyWindow.rootViewController);
NSLog(@"delegate.window --> %@",[UIApplication sharedApplication].delegate.window.rootViewController);
NSLog(@"self.view.window -> %@",self.view.window.rootViewController);
}
輸出是:
keyWindow --------> (null)
delegate.window --> <ViewController: 0x10030c0e0>
self.view.window -> (null)
keyWindow --------> <UIApplicationRotationFollowingController: 0x100204510>
delegate.window --> <ViewController: 0x10030c0e0>
self.view.window -> <ViewController: 0x10030c0e0>
當viewDidLoad
,實際上該窗口還沒有準備好,所以沒有爲系統窗口。 UIAlertView
可能會主導窗口,所以你不能得到你想要的窗口。
Downvoted,因爲這是不正確的,請參閱傑弗裏托馬斯和安迪達爾文的答案。 –