在 UIAlertView 和 UIActionSheet 的代理方法中給 [UIApplication sharedApplication].keyWindow 設置 rootViewController 時,假如使用下面代碼:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
TheViewController *vc = [[TheViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = vc;
}
}
And
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
TheViewController *vc = [[TheViewController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = vc;
}
}
Then,在程序執(zhí)行到這里的時候,你會發(fā)現(xiàn),界面雖然會跳轉到 TheViewController,但是在一眨眼間又跳轉回原來的界面。
解決辦法:#####
1、要先導入 AppDelegate
#import "AppDelegate.h"
2、使用下面代碼來實現(xiàn)
TheViewController *vc = [[TheViewController alloc] init];
((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController = vc;