一般來說 [UIApplication sharedApplication].keyWindow.rootViewController 會在 appDelegate 中初始化,並且整個應用運行過程中都不會變。
所以在很多地方需要用 presentModelViewController 方法時,用這個 rootViewController 是很方便的,因為它相當於一個全局變量,無需再申請、釋放。
但是本人在使用過程中發現有 rootViewController 被改為 nil 的情況。
分析后發現,當 UIAlertView 調用 show 方法后,rootViewController 會被改為 nil,當它 dismiss 后又改回去。而我需要用戶在點擊 alertview 上的按鈕后再做
presentModelViewController 的操作。
解決方法:
不要使用 alertview delegate 中的
- (void)alertView:clickedButtonAtIndex:
方法
而要用:
- (void)alertView:diddismissWithButtonAtIndex:
方法!