关于 iOS启动图控制器的一些发现 姿势
来源:互联网 发布:江苏省人工智能学会 编辑:程序博客网 时间:2024/04/30 08:51
ParentViewController上子ViewController转换时的方法调用顺序
使用first和second之间转场方法transitionFromViewController调用的一系列方法的顺序
首ParentViewController的代码:
- (void)viewDidLoad {//first和second这两个子ViewController上面的按钮会发送通知给Parent进行转场
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(dealWithFirstClick) name:@"firstClick"object:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(dealWithSecondClick) name:@"secondClick"object:nil];
[superviewDidLoad];
//生俩儿子
first = [[FirstViewControlleralloc] initWithNibName:@"FirstViewController"bundle:nil];
second = [[SecondViewControlleralloc] initWithNibName:@"SecondViewController"bundle:nil];
//启动后先添加first,大儿子
[self addChildViewController:first];
[self.view addSubview:first.view];
// Do any additional setup after loading the view, typically from a nib.
}
然后是重点了,FirstViewController先加载视图,上面有一个按钮,点击了后会跳转到二儿子SecondViewController
- (void)dealWithFirstClick
{
//调用addChildViewController之前会自动调用second的willMoveToParentViewController
[self addChildViewController:second];
[self transitionFromViewController:firsttoViewController:second duration:0.5
options:UIViewAnimationOptionTransitionFlipFromTopanimations:nil
completion:^(BOOL finished){
//addChildViewController之后手动添加的didMoveToParentViewController
[second didMoveToParentViewController:self];
//删除之前first之前手动添加的willMoveToParentViewController
[first willMoveToParentViewController:nil];
//调用removeFromParentViewController之后会自动调用first的didMoveToParentViewController
[first removeFromParentViewController];
}];
}
上面的代码体会一下,addChild和removeFromParent这两件事,这两个核心方法会连带着两个子ViewController分别执行willMove和didMove,也就是一共四个方法,顺序如同我上面注释所说,有两个是自动实现的,有两个是我手动添加的,这个顺序需要体会一下,容易晕,当然不调用didMove和willMove也没问题,我就是写在这里记录下顺序。同理second上面的按钮点击了回到first代码如下:
- (void)dealWithSecondClick
{
[self addChildViewController:first];
[self transitionFromViewController:secondtoViewController:first duration:0.5
options:UIViewAnimationOptionTransitionFlipFromRightanimations:nil
completion:^(BOOL finished){
[first didMoveToParentViewController:self];
[second willMoveToParentViewController:nil];
[second removeFromParentViewController];
}];
}
总结:从first到second的dealWithFirstClick方法执行顺序
1.willMoveToParentViewController //first 执行 (AUTO)
2.addChildViewController //Parent执行(MANUAL)
3.didMoveToParentViewController //first 执行(MANUAL)
4.willMoveToParentViewController //second执行(MANUAL)
5.removeFromParentViewController //Parent执行(MANUAL)
6.didMoveToParentViewController //Second执行 (AUTO)
肯定是会晕的,但是细心观察一下就是这么个执行顺序
注意:
关于willMoveToParentViewController方法和didMoveToParentViewController方法的使用
1.这两个方法用在子试图控制器交换的时候调用!即调用transitionFromViewController方法时,调用。
2.当调用willMoveToParentViewController方法或didMoveToParentViewController方法时,要注意他们的参数使用:
当某个子视图控制器将从父视图控制器中删除时,parent参数为nil。
即:[将被删除的子试图控制器 willMoveToParentViewController:nil];
当某个子试图控制器将加入到父视图控制器时,parent参数为父视图控制器。
即:[将被加入的子视图控制器 didMoveToParentViewController:父视图控制器];
3.无需调用[子视图控制器 willMoveToParentViewController:父视图控制器]方法。因为我们调用[父视图控制器 addChildViewController:子视图控制器]时,已经默认调用了。
只需要在transitionFromViewController方法后,调用[子视图控制器didMoveToParentViewController:父视图控制器];
addChildViewController: will call [child willMoveToParentViewController:self] before adding the child.
However, it will not call didMoveToParentViewController:.
It is expected that a container view controller subclass will make this call after a transition to the new child has completed or, in the case of no transition, immediately after the call to addChildViewController:.
4.无需调用[子视图控制器 didMoveToParentViewController:父视图控制器]方法。因为我们调用
[子视图控制器 removeFromParentViewController]时,已经默认调用了。
只需要在transitionFromViewController方法之前调用:[子视图控制器willMoveToParentViewController:nil]。
- 关于 iOS启动图控制器的一些发现 姿势
- 关于 iOS启动图的一些发现
- 关于反射的一些姿势
- 关于二分图的姿势
- ios关于控制器的生命周期
- 突然发现的新姿势
- 发现的一些关于python的文章
- 关于Spring整合发现的一些问题。
- 关于Spring整合发现的一些问题。
- 关于二值化函数cvAdaptiveThreshold的一些发现
- 关于angular2更新时机的一些发现
- iOS 启动图片会影响控制器的启动大小
- iOS关于导航控制器
- tomcat的正确启动姿势
- maven项目的一些姿势
- scanf() 的一些新姿势
- 关于unity3d动画控制器的一些小工具的写法
- iOS 创建对象的姿势
- 《Java源码分析》:CyclicBarrier(part two)
- UIButton
- Android学习工作各种资源
- 还原二叉树
- 下列有关windows系统的EXE和DLL文件说法错误的是?
- 关于 iOS启动图控制器的一些发现 姿势
- python string字符串的7种连接方式
- HDU4461 The Power of Xiangqi
- spring-mvc跳转根目录下的jsp文件
- Permission denied: make_sock: could not bind to address [::]:80
- windows安装composer方法和使用方法
- Centos 6.5 配置连接Windows远程桌面
- 前言
- JavaScript笔记——this的取值