oc addChildViewController
实现类似于新闻端多个标签页。
此处转载于 http://www.tuicool.com/articles/3ymMzub
@interface HMTMainViewController () <UIScrollViewDelegate>@property (nonatomic ,strong) HMTThirdViewController *thirdVC;
@property (nonatomic ,strong) HMTFirstViewController *firstVC;
@property (nonatomic ,strong) HMTSecondViewController *secondVC;@property (nonatomic ,strong) UIViewController *currentVC;@property (nonatomic ,strong) UIScrollView *headScrollView; // 顶部滚动视图@property (nonatomic ,strong) NSArray *headArray;@end@implementation HMTMainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. self.navigationItem.title = @"网易新闻Demo";
self.headArray = @[@"头条",@"娱乐",@"体育",@"财经",@"科技",@"NBA",@"手机"];
/**
* automaticallyAdjustsScrollViewInsets 又被这个属性坑了
* 我"UI高级"里面一篇文章着重讲了它,大家可以去看看
*/
self.automaticallyAdjustsScrollViewInsets = NO;
self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
self.headScrollView.backgroundColor = [UIColor purpleColor];
self.headScrollView.contentSize = CGSizeMake(560, 0);
self.headScrollView.bounces = NO;
self.headScrollView.pagingEnabled = YES;
[self.view addSubview:self.headScrollView];
for (int i = 0; i < [self.headArray count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0 + i*80, 0, 80, 40);
[button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i + 100;
[button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.headScrollView addSubview:button];
}
/*
苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。
对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。
这样做的好处:
1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。
2.当某个子View没有显示时,将不会被Load,减少了内存的使用。
3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。
*/
/**
* 在iOS5中,ViewController中新添加了下面几个方法:
* addChildViewController:
* removeFromParentViewController
* transitionFromViewController:toViewController:duration:options:animations:completion:
* willMoveToParentViewController:
* didMoveToParentViewController:
*/
self.firstVC = [[HMTFirstViewController alloc] init];
[self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];
[self addChildViewController:_firstVC];
self.secondVC = [[HMTSecondViewController alloc] init];
[self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];
//这段代码会触发viewdidload
self.thirdVC = [[HMTThirdViewController alloc] init];
[self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];
// 默认,第一个视图(你会发现,全程就这一个用了addSubview)
[self.view addSubview:self.firstVC.view];
self.currentVC = self.firstVC;
//addSubView后相应的VC viewWillAppear将会调用。前提是VC已经加入本VC。
}- (void)didClickHeadButtonAction:(UIButton *)button
{
// 点击处于当前页面的按钮,直接跳出
if ((self.currentVC == self.firstVC && button.tag == 100)||(self.currentVC == self.secondVC && button.tag == 101.)) {
return;
}else{
// 展示2个,其余一样,自行补全噢
switch (button.tag) {
case 100:
[self replaceController:self.currentVC newController:self.firstVC];
break;
case 101:
[self replaceController:self.currentVC newController:self.secondVC];
break;
case 102:
//.......
break;
case 103:
//.......
break;
case 104:
//.......
break;
case 105:
//.......
break;
case 106:
//.......
break;
//.......
default:
break;
}
}}// 切换各个标签内容
- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
/**
* 着重介绍一下它
* transitionFromViewController:toViewController:duration:options:animations:completion:
* fromViewController 当前显示在父视图控制器中的子视图控制器
* toViewController 将要显示的姿势图控制器
* duration 动画时间(这个属性,old friend 了 O(∩_∩)O)
* options 动画效果(渐变,从下往上等等,具体查看API)
* animations 转换过程中得动画
* completion 转换完成
*/
[self addChildViewController:newController];
[self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
//调用上边方法后,相应VC的viewWillDisapear和viewWillApear将会调用。
if (finished) {
[newController didMoveToParentViewController:self];
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
self.currentVC = newController;
}else{
self.currentVC = oldController;
}
}];
}
![](http://img2.tuicool.com/eEJZJj.png!web)
oc addChildViewController相关推荐
- IOS开发基础之微博项目第1天-OC版
IOS开发基础之微博项目第1天-OC版 纯代码创建的项目,具有参考价值 该资料来自2014年7月3号,虽然时间过去较长,但是oc和swift不同,oc语法迭代更新慢 具有一定的参考意义 涉及xib加载 ...
- OC 组合实现多继承
OC无法完全先C++使用多继承,但可以采用组合的模式来代替继承模式.(协议实现)实现多继承的代码:举例现在ClassC需要继承ClassA中methodA.ClassB中methodB,具体的代码为: ...
- OC实用转换model的工具
OC实用转换model的工具 说明 这是本人写的一个专门用来将json数据直接转换生成Model文件的工具,目的是为了让你从写Model文件的繁琐过程中解脱出来,提升效率以及减少出错的几率,工具的特点 ...
- OC学习笔记之Foundation框架NSNumber、NSValue和NSDate(转)
一.NSNumber OC数组类NSArray,它只能存放 OC的对象,对于基本的数据类型确无能为力,但是实际编程中经常要把基本的数据如int.float,结构体存放的OC数组中,怎么办?这里的 NS ...
- OC学习篇之---对象的拷贝
在前一篇文章中我们说到了如何解决对象的循环引用问题:http://blog.csdn.net/jiangwei0910410003/article/details/41926369,这一篇文章我们就来 ...
- OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法
一:NSArray 和NSMutableArray 1: NSArray:不可变数组 NSArray是OC中使用的数组,只能用来存放OC对象,不能存放非OC对象如基本数据类型 它使不可变的,一旦初始化 ...
- oc 协议 回调 静态成员_OC底层原理探究:Category、关联对象和block本质
1.分类Category的使用 // 给MJPerson类添加分类 @interface MJPerson : NSObject - (void)run; @end@implementation MJ ...
- 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js
iOS js oc相互调用(JavaScriptCore) 从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这个框 ...
- 6 OC 中的isa 指针
目录 一 isa 指针 二 类对象中的superclass 一 isa 指针 isa 指针 ,OC 中的对象都是有的 如下图所示,实例对象isa 指针指向 类对象,类对象的isa 指针指向 元类对象 ...
最新文章
- testng入门_单元测试
- ubuntu下小键盘不能用
- Intel Realsense D435小车避障 摄像头在高于一定平面h时的深度map公式计算(深度d与视场角视角α的关系)
- 印象笔记无法连服务器(internet explore的问题)
- matlab数组元素对应相减,MATLAB下矩阵的运算 看完你就知道了
- windows 编程 —— 消息与参数(定时器、初始化消息、改变大小)
- Flink 在小红书推荐系统中的应用
- java在线预览txt、word、ppt、execel,pdf代码
- linux top 命令可视化_Linux top命令使用详解:显示或管理执行中的程序
- mysql event 简单demo
- 比起月薪过万的程序员大多数人居然选择了月薪3K的公务员?
- linux动态ip 搭建什么服务,怎么给Linux系统配置动态IP服务
- 再品Resnet残差网络
- linux建ftp用户限制访问,Linux下建ftp用户并限制用户访问路径
- Mysql 日志管理详解
- 代谢组学常见数据库列表和简介
- 不知道Word转图片PDF怎么转?1分钟帮你快速转换
- SIP协议的功能在统一通信中的体现
- 设置TextView滚动
- mac环境下cocos2dx引擎3.x版本的创建工程步骤