[xxx.navigationController pushViewController:yyy animated:YES/NO];
[xxx.navigationController popViewControllerAnimated:YES?NO];
以及
[xxx presentViewController:yyy animated:YES/NO completion:nil];
[xxx dismissViewControllerAnimated:YES/NO completion:nil];
以上几个关于视图转换的方法我们在开发过程中经常用到,但是在这些方法在执行完成之后各个viewController之间是以什么样的关系存在的,没有具体的想过,之前也看过一篇关于相关内容的文章,时间久了再想看时文章已经找不到了,所以这次打算写代码测一下结果,并总结记录下来:
UIViewController分为两种类型:
viewController容器,如:UITabBarController、UINavigationController等
viewcontroller:UIViewController及其自定义的子类
viewController之间存在两种关系
1:包含关系,比如viewControllerA的parentViewCONtroller是viewControllerB
2:presented与presenting关系,如viewControllerA调用presentViewController:animated:completion:方法后viewControllerB被展示出来,这时viewControllerA的presentedViewController是viewControllerB,而viewControllerB的presentingViewController是viewControllerA
我们先说一下包含关系代码如下:
-(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController tabBarController = [[UITabBarController alloc] init];
UINavigationController navigationController = [[UINavigationController alloc] init];
ViewController *viewController = [[ViewController alloc] init];
[navigationController addChildViewController:viewController];
[tabBarController addChildViewController:navigationController];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
结果就不详细解释了关系图如下:
presented 与 presenting关系代码如下:
ViewController viewControllerA = [[ViewController alloc] init];
ViewController viewControllerB = [[ViewController alloc] init];
[viewControllerA presentViewController:viewControllerB animated:YES completion:nil];
关系见下图:
如果两种关系同时存在关系比较复杂,见下图: