[UIApplication sharedApplication].keyWindow.rootViewController

一般来说 [UIApplication sharedApplication].keyWindow.rootViewController 会在 appDelegate 中初始化,并且整个应用运行过程中都不会变。

所以在很多地方需要用 presentModelViewController 方法时,用这个 rootViewController 是很方便的,因为它相当于一个全局变量,无需再申请、释放。

但是本人在使用过程中发现有 rootViewController 被改为 nil 的情况。

分析后发现,当 UIAlertView 调用 show 方法后,rootViewController 会被改为 nil,当它 dismiss 后又改回去。而我需要用户在点击 alertview 上的按钮后再做

presentModelViewController 的操作。

解决方法:

不要使用 alertview delegate 中的 

- (void)alertView:clickedButtonAtIndex:

方法

而要用:

- (void)alertView:diddismissWithButtonAtIndex:

方法!

原文地址:https://www.cnblogs.com/duzhaoquan/p/6026149.html