美文网首页codeER.tec
在 UIAlertView 和 UIActionSheet 的代

在 UIAlertView 和 UIActionSheet 的代

作者: 馬夫 | 来源:发表于2016-04-19 10:16 被阅读219次

    UIAlertViewUIActionSheet 的代理方法中给 [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,在程序执行到这里的时候,你会发现,界面虽然会跳转到 TheViewController,但是在一眨眼间又跳转回原来的界面。

    解决办法:#####

    1、要先导入 AppDelegate

    #import "AppDelegate.h"
    

    2、使用下面代码来实现

    TheViewController *vc = [[TheViewController alloc] init];
    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController = vc;

    相关文章

      网友评论

      • 曾经像素有点低:是不是因为alertView也在keyWindow上导致的
        曾经像素有点低:@馬夫 终于明白了,谢谢啦:smiley:
        馬夫:[alertView show] 的时候,alertView 就成了 keyWindow。
        代理方法里面设置了 rootViewController。但当代理方法走完时,alertView被释放,keyWindow回到之前的了。
      • 曾经像素有点低:哈,刚好今天遇到了这个问题,找不到原因,后来考虑到了是alert代理中设置rootViewController的问题,但是又不能确定。百度一搜就看到这篇文章。完美解决!
        学习啦,作者能帮忙具体讲一讲一下原因吗?实在是搞不懂为什么:stuck_out_tongue_winking_eye:

      本文标题:在 UIAlertView 和 UIActionSheet 的代

      本文链接:https://www.haomeiwen.com/subject/hinalttx.html