zoukankan      html  css  js  c++  java
  • ios调用dismissViewController的一个小陷阱

    我们的APP从启动到进入主页面。是通过presentViewController构造了一个ViewController序列,类似于首页 -> 登陆页 -> 启动载入页 -> 主页面

    当中。在启动载入页的viewDidAppear方法里做了非常多逻辑处理:

    -(void) viewDidAppear:(BOOL)animated{
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            
            clientInfo = [YLSClientInfo new];
            
            if([clientInfo needInit]){
                [self mkdirAndDatabaseFile];
            }else{
                [self refreshVersion:[clientInfo currentVersion]];
            }
            
           // 各种处理逻辑
        });
    }

    然后进入主页面之后,假设用户退出登陆,就须要回到首页,所以会在首页上调用dismissViewController方法。原先的代码类似这样:

    UIViewController *origin = self.presentingViewController.presentingViewController;
    if([origin isMemberOfClass:[YLSLoginViewController class]]){
        origin = self.presentingViewController.presentingViewController.presentingViewController;
    }
    [origin dismissViewControllerAnimated:NO completion:nil];

    预期的结果是。直接回到首页。然后触发首页的viewDidAppear方法。

    实际上通过观察console warning才发现,中间启动载入页的viewDidAppear方法也被调用了。登陆页因为没有写viewDidAppear方法,所以没有发现,但我推測假设有的话,也一样会被调用。似乎ViewController是依照顺序一个接一个出栈的。所以每个“之前的”ViewController的viewDidAppear方法应该都会被触发

    查了一下API,又上stackoverflow搜索了半天。似乎没有办法阻止这个默认行为。所以最后我的解决的方法是在中间的Controller上加了标记:

    -(void) viewDidAppear:(BOOL)animated{
        
        // 假设是因为调用了dismiss而触发了此方法,不进行初始化
        if(self.isDismissing){
            return;
        }
        
       // 初始化载入逻辑
    }

    YLSBootstrapViewController *bootstrapController = (YLSBootstrapViewController*)self.presentingViewController;
    bootstrapController.isDismissing = YES;
                    
    UIViewController *origin = self.presentingViewController.presentingViewController;
    if([origin isMemberOfClass:[YLSLoginViewController class]]){
        origin = self.presentingViewController.presentingViewController.presentingViewController;
    }
    [origin dismissViewControllerAnimated:NO completion:nil];

    不知道大家有没有更好的做法

  • 相关阅读:
    Visual Studio.Net 已检测到指定的Web服务器运行的不是ASP.NET1.1版。您将无法运行ASP.NET Web应用程序或服务。
    ActiveX 部件不能创建对象
    XML Schema帮你建模
    XBRL CHINA - XBRL入门
    Xml文档验证—编程篇
    怎样用XML技术对数据库进行操作
    Vclskin 问答
    Altova XMLSpy Ent 2006 汉化特别版 破解版
    一个通过DataSet操作XML的类
    XML Schema教程
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5218233.html
Copyright © 2011-2022 走看看