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];

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

  • 相关阅读:
    Mysql表设计需要注意的问题
    Mysql开始root远程访问权限
    python学习之简介
    常用的Git命令
    Java虚拟机类加载机制
    java垃圾回收机制的理解
    解析和操作XML文件
    Hibernate的工作流程以及三种状态(面试题)
    SSH整合
    Spring事务管理的注解方式
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5218233.html
Copyright © 2011-2022 走看看