每天早上打開(kāi)電腦瘤礁,例行檢查崩潰情況統(tǒng)計(jì),發(fā)現(xiàn)了一個(gè)新bug梅尤。然而看了代碼柜思,貌似沒(méi)問(wèn)題啊,后來(lái)研究一番發(fā)現(xiàn)了這個(gè)bug克饶。話不多說(shuō)酝蜒,上代碼。矾湃。亡脑。
看起來(lái)上段代碼沒(méi)有什么問(wèn)題,可是當(dāng)UIAlertView的出現(xiàn)的同時(shí)然后調(diào)圖中的代碼邀跃,就出現(xiàn)UIApplicationRotationFollowingController霉咨。因?yàn)閁IAlertView出現(xiàn)生成了一個(gè)新的window,加在了界面上面拍屑。在新建的window上展示的途戒,所以keyWindow已經(jīng)改變了,無(wú)法通過(guò)[UIApplicationsharedApplication].keyWindow獲取rootViewController僵驰。這個(gè)時(shí)候獲取到的keyWindow就是UIAlertControllerShimPresenterWindow喷斋。而此時(shí)keyWindow的rootViewController就是UIApplicationRotationFollowingController唁毒。然后當(dāng)然了,找不到方法導(dǎo)致崩潰星爪。
解決辦法如下:
1浆西、首先,獲取rootViewController方式應(yīng)該為: UITabBarController *tab =(UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2顽腾、判斷類型是否正確: if (tab && [tab isKindOfClass:[UITabBarController class]]) { tab.selectedIndex = 2;; }