segmentControl实现控制器的切换
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1 创建窗口
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
//2 设置主控制器
XCMainController *mainVc = [[XCMainController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVc];
self.window.rootViewController = nav;
//3 显示window
[self.window makeKeyAndVisible];
return YES;
}
第一个控制器初始化view:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"fristController";
label.font = [UIFont systemFontOfSize:17];
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
}
第二个控制器初始化view:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UILabel *label = [[UILabel alloc] init];
label.text = @"secondController";
label.font = [UIFont systemFontOfSize:17];
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
}
主控制器逻辑实现
添加子控制器
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationItem.titleView = [self setupSegment];
self.fristVc = [[XCFristController alloc] init];
self.fristVc.view.frame = CGRectMake(0, navigationHeight, mainVcWidth, mainVcHeight - 64);
[self addChildViewController:_fristVc];
self.secondVc = [[XCSecondController alloc] init];
self.secondVc.view.frame = CGRectMake(0, navigationHeight, mainVcWidth, mainVcHeight - 64);
[self addChildViewController:_secondVc];
//设置默认控制器为fristVc
self.currentVC = self.fristVc;
[self.view addSubview:self.fristVc.view];
}
初始化UISegmentControl:
/**
* 初始化segmentControl
*/
- (UISegmentedControl *)setupSegment{
NSArray *items = @[@"1", @"2"];
UISegmentedControl *sgc = [[UISegmentedControl alloc] initWithItems:items];
//默认选中的位置
sgc.selectedSegmentIndex = 0;
//设置segment的文字
[sgc setTitle:@"oneView" forSegmentAtIndex:0];
[sgc setTitle:@"twoView" forSegmentAtIndex:1];
//监听点击
[sgc addTarget:self action:@selector(segmentChange:) forControlEvents:UIControlEventValueChanged];
return sgc;
}
监听segmentControl点击事件:
- (void)segmentChange:(UISegmentedControl *)sgc{
//NSLog(@"%ld", sgc.selectedSegmentIndex);
switch (sgc.selectedSegmentIndex) {
case 0:
[self replaceFromOldViewController:self.secondVc toNewViewController:self.fristVc];
break;
case 1:
[self replaceFromOldViewController:self.fristVc toNewViewController:self.secondVc];
break;
default:
break;
}
}
控制器切换
/**
* 实现控制器的切换
*
* @param oldVc 当前控制器
* @param newVc 要切换到的控制器
*/
- (void)replaceFromOldViewController:(UIViewController *)oldVc toNewViewController:(UIViewController *)newVc{
/**
* transitionFromViewController:toViewController:duration:options:animations:completion:
* fromViewController 当前显示在父视图控制器中的子视图控制器
* toViewController 将要显示的姿势图控制器
* duration 动画时间(这个属性,old friend 了 O(∩_∩)O)
* options 动画效果(渐变,从下往上等等,具体查看API)UIViewAnimationOptionTransitionCrossDissolve
* animations 转换过程中得动画
* completion 转换完成
*/
[self addChildViewController:newVc];
[self transitionFromViewController:oldVc toViewController:newVc duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
if (finished) {
[newVc didMoveToParentViewController:self];
[oldVc willMoveToParentViewController:nil];
[oldVc removeFromParentViewController];
self.currentVC = newVc;
}else{
self.currentVC = oldVc;
}
}];
}
segmentControl实现控制器的切换的更多相关文章
- iOS 开发笔记-控制器tab切换view显示
在开发过程中,我们常常会碰到一种情况就是,在一个controller里面,经常要放很多复杂的控制,最常用的就是tar切换.tar切换,原理就是在一个controller里面,显示另一个controll ...
- iOS边练边学--父子控制器之自定义控制器的切换
一.如图所示的界面,按钮One.Two.Three分别对应三个控制器的view,点击实现切换.个人感觉父子控制器的重点在于,控制器的view们之间建立了父子关系,控制器不建立的话,发生在view上面的 ...
- CI框架在控制器中切换读写库和读写库
CodeIgniter框架版本:3.1.7 ,php版本:5.6.* ,mysql版本:5.6 在Ci框架中,可以在application/config/database.php中配置多个group, ...
- 自定义视图控制器切换(iOS)
在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...
- iOS - 切换rootViewController时,销毁之前的控制器
一.iOS在切换根控制器时,如何销毁之前的控制器?(切换rootViewController时注意的内存泄漏) 首先.在iOS的ARC机制下,任何对象,当没有其他对象对他进行强引用时,都会被自动释放. ...
- IOS UINavigationController 导航控制器
/** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...
- UIVIewController自定义切换效果-b
之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...
- ios视图切换之push与present混用
在变成过程中,经常遇到两个视图控制器之间的切换,导航控制器即UINaVigation是最常用的一种,有时为了某些效果又需要进行模态切换,即present. 我们的布局经常是在window上加一个nav ...
- CI框架在模型中切换读写库和读写库
如果你想在控制器中切换在application/config/database.php中配置好的数据库group,那么你可以参考这篇博客:CI框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...
随机推荐
- MySQL的简单查询语句
查询: 一:查询所有数据 select * from Info 查所有数据 select Code,Name from Info 查特定列 二:根据条件查 select * from Info whe ...
- jqueryValidation使用
jq form表单前端校验可以使用jq插件jquery-validation.js.具体的使用方法: 1.引入文件: <link rel="stylesheet" href= ...
- SQL 存储过程优化经验
经现场同事反映,他们用的好好的XML 导出工具最近一直报错,经常报数据库连接超时,查看数据库发现已经有100G 以上有空间了. 但导出数据的存储过程里面每次按时间只导1000多条数据,近理说有时间过滤 ...
- C#高级--通过类来理解学习委托
namespace classanddelegate { class Program { static void Main(string[] args) { //这是类的实例化 Test test = ...
- MongoDB基本管理命令
MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表:而每个集合中 可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由 ...
- css高级应用及问题记录(持续更新)
css 参考手册: 1.http://css.doyoe.com/ 1.混合选择器样式定义: .button.icon:before { content: ""; po ...
- JQuery初体验
虽然做b/s也有一年半了,但是还没怎么认真的去看JQuery,趁自己生病的这几天,恶补一下JQuery方面的知识,保持学习的态度,内容很简单,聊以自慰一下>_<.废话不多说,直接上代码了. ...
- 在安卓下打包cocos2d-js 3.6项目with ProtoBuf.js
项目用到了cocos2d-js 3.6和ProtoBuf.js,但是打包成apk时运行时总是报错(evaluatedOK == JS_FALSE),没有具体的文件和行号报错信息. 只能一个一个文件排查 ...
- configure: error: no acceptable C compiler found in $PAT 的解决方案
configure: error: no acceptable C compiler found in $PATH See `config.log' for more details.你的机器里没有安 ...
- 【转】如何在Windows+VS2005使用最新静态libcurl 7.35.0获取网页数据,支持HTTPS
地址: http://blog.csdn.net/hujkay作者:Jekkay Hu(34538980@qq.com)关键词:Windows,curl,ssl, visual c++ 2005, ...