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,因为这是不正确的,请参阅杰弗里托马斯和安迪达尔文的答案。 –