ios调用dismissViewController的一个小陷阱
来源:互联网 发布:dota牛蛙淘宝店 编辑:程序博客网 时间:2024/05/18 00:10
我们的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];
不知道大家有没有更好的做法
0 0
- ios调用dismissViewController的一个小陷阱
- Java的一个小陷阱
- ListView的一个小陷阱!
- accept的一个小陷阱
- map使用时的一个小陷阱
- Class.getClassLoader()的一个小陷阱:)
- scanf使用的一个小陷阱
- 一个PHP语言的小陷阱
- 调用scanf函数的一个陷阱
- 【基础】程序中遇到的一个小陷阱
- gcc中使用scanf的一个小陷阱
- Python函数默认参数的一个小陷阱
- 警惕!Unity3D中UnityEngine.Object的一个小陷阱
- java获取系统时间的一个小陷阱
- Python函数默认参数的一个小陷阱
- Windows下配置apache的一个小陷阱
- 关于全局变量和局部变量的一个小陷阱
- sql中的if else 的一个小陷阱
- [Leetcode]String to Integer (atoi)
- Spring 事务隔离级别
- 工作过程中学到的正则表达式
- C语言中Uint8_t数据类型
- XML解析的三种方式总结
- ios调用dismissViewController的一个小陷阱
- 【cocos2dx网络游戏】搭建CS架构的基本通信框架(一)server
- NSArray 和 NSMutableArray 定义和基本用法
- Download interrupted: URL not found.
- Redis的安装、配置
- Android监听外部存储设备的状态(SD卡、U盘等等)
- json_ajax交互
- poj 1041 John's trip 欧拉回路
- jvisualvm远程监控Tomcat