IOS—通过ChildViewController实现view的切换
IOS—通过ChildViewController实现view的切换
在以前,一个UIViewController的View可能有很多小的子view。这些子view很多时候被盖在最后,我们在最外层ViewController的viewDidLoad方法中,用addSubview增加了大量的子view。这些子view大多数不会一直处于界面上,只是在某些情况下才会出现,例如登陆失败的提示view,上传附件成功的提示view,网络失败的提示view等。但是虽然这些view很少出现,但是我们却常常一直把它们放在内存中。另外,当收到内存警告时,我们只能自己手工把这些view从super view中去掉。
我们现在可以使用下面的方法:
addChildViewController: //添加
removeFromParentViewController //删除
transitionFromViewController:toViewController:duration:options:animations:completion://转换
willMoveToParentViewController: //当一个视图控制器从视图控制器容器中被添加或者被删除之前,该方法被调用
didMoveToParentViewController://当从一个视图控制容器中添加或者移除viewController后,该方法被调用。
这样,就能够将一个页面中的UIViewController控制起来,而不是混乱的共用一个UIViewController
,最重要的是,编程习惯的革命:降低了功能的耦合度!
下面是childViewController的一个简单使用例子
//在parent view controller 中添加 child view controller
   FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [self addChildViewController:firstViewController];
    SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self addChildViewController:secondViewController];
    ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self addChildViewController:thirdViewController];
    [self.view  addSubview:thirdViewController.view];
  // addChildViewController回调用[child willMoveToParentViewController:self] ,但是不会调用didMoveToParentViewController,所以需要显示调用
    [thirdViewController didMoveToParentViewController:self];
    currentViewController=thirdViewController;
  //切换child view controller
     [self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
            }  completion:^(BOOL finished) {
               //......
            }];
    currentViewController=firstViewController;
  //移除child view controller
    // removeFromParentViewController在移除child前不会调用[self willMoveToParentViewController:nil] ,所以需要显示调用
    [currentViewController willMoveToParentViewController:nil];
    [currentViewController removeFromSuperview];
    [currentViewController removeFromParentViewController];IOS—通过ChildViewController实现view的切换的更多相关文章
- [转]IOS Segment页面之间view的切换
		有三个view,分别为view1.view2.view3,通过UISegmentedControl进行三个view的切换. @interface UIViewDemoViewController : ... 
- IOS中两个view的切换
		在ios中,rootview为PassWordViewController,secondview为SecondViewController,实现在rootview中听过一个跳转按钮实现跳转到secon ... 
- ios中两个view动画切换
		@interface ViewController () @property(nonatomic,retain)UIView *redview; @property(nonatomic,retain) ... 
- (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解
		原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: ... 
- 【IOS界面布局】横竖屏切换和控件自适应(推荐)
		[IOS界面布局]横竖屏切换和控件自适应(推荐) 分类: [MAC/IOS下开发]2013-11-06 15:14 8798人阅读 评论(0) 收藏 举报 横竖屏切换 自适应 第一种:通过人为的办法改 ... 
- iOS开发:使用Tab Bar切换视图
		iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ... 
- iOS 容器控制器 (Container View Controller)
		iOS 容器控制器 (Container View Controller) 一个控制器包含其他一个或多个控制器,前者为容器控制器 (Container View Controller),后者为子控制器 ... 
- 转:iOS应用程序生命周期(前后台切换,应用的各种状态)详解
		iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: iOS开发进阶2012-10-08 15:35 42691人阅读 评论(30) 收藏 举报 iosapplication任务anima ... 
- IOS 开发中  Whose view is not in the window hierarchy  错误的解决办法
		在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ... 
随机推荐
- WPF2D绘制图形方法
			我们先看看效果如何: xaml文件: <Window x:Class="WPF2D绘制图形方法.MainWindow" xmlns="http://schemas. ... 
- id类型
			id类型 在Objective-C 中,id 类型是一个独特的数据类型.在概念上,类似Java 的Object 类,可以转换为任何数据类型.换句话说,id 类型的变量可以存放任何数据类型的对象.在内部 ... 
- [译]36 Days of Web Testing(六)
			Day 30 Test in situ 真实场景下的测试 为什么? 我十分推崇现场测试,简单讲就是要在你的站点或应用真实使用的场景下进行测试.但随着人口增长,对于"真实场景"的定 ... 
- KMP字符串模式匹配详解(转)
			来自CSDN A_B_C_ABC 网友 KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n);KMP匹配算法.可以证明它的时间复杂度 ... 
- Android MAVEN项目标准目录结构
			1.标准目录结构: src -main –bin 脚本库 –java java源代码文件 –resources 资源库,会自动复制到classes目录里 –fi ... 
- poj1849
			不难发现每条边最多走两次,最少走一次也就是我们要在所有走两次的边中选两条从根出发没有公共边的路径使路径上的边少走一次显然我们找的是最长路径 
- 小细节:Java中split()中的特殊分隔符 小数点
			这两天做项目过程中由于数据表字段设计的太恶心了,导致自己填坑 关于微信支付和支付宝的支付有一个不同点:就是金额的处理,支付宝金额的单位是0.01元,但是微信支付中1表示0.01元,当时设计价格的时候使 ... 
- Spark(Hive) SQL中UDF的使用(Python)
			相对于使用MapReduce或者Spark Application的方式进行数据分析,使用Hive SQL或Spark SQL能为我们省去不少的代码工作量,而Hive SQL或Spark SQL本身内 ... 
- java  学习连接
			@Repository.@Service.@Controller 和 @Component 注解:http://blog.csdn.net/ye1992/article/details/19971 ... 
- MIPI D-PHY 总结
			Operating Modes: Control, High-Speed, and Escape 1.The Lane is only in High-Speed mode during Data b ... 
