- (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实现控制器的切换的更多相关文章

  1. iOS 开发笔记-控制器tab切换view显示

    在开发过程中,我们常常会碰到一种情况就是,在一个controller里面,经常要放很多复杂的控制,最常用的就是tar切换.tar切换,原理就是在一个controller里面,显示另一个controll ...

  2. iOS边练边学--父子控制器之自定义控制器的切换

    一.如图所示的界面,按钮One.Two.Three分别对应三个控制器的view,点击实现切换.个人感觉父子控制器的重点在于,控制器的view们之间建立了父子关系,控制器不建立的话,发生在view上面的 ...

  3. CI框架在控制器中切换读写库和读写库

    CodeIgniter框架版本:3.1.7 ,php版本:5.6.* ,mysql版本:5.6 在Ci框架中,可以在application/config/database.php中配置多个group, ...

  4. 自定义视图控制器切换(iOS)

    在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...

  5. iOS - 切换rootViewController时,销毁之前的控制器

    一.iOS在切换根控制器时,如何销毁之前的控制器?(切换rootViewController时注意的内存泄漏) 首先.在iOS的ARC机制下,任何对象,当没有其他对象对他进行强引用时,都会被自动释放. ...

  6. IOS UINavigationController 导航控制器

    /** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...

  7. UIVIewController自定义切换效果-b

      之前介绍动画时提过UIView的转场动画,但是开发中我们碰到更多的viewController的切换,ios中常见的viewcontroller切换有四种:模态视图,导航栏控制器,UITabBar ...

  8. ios视图切换之push与present混用

    在变成过程中,经常遇到两个视图控制器之间的切换,导航控制器即UINaVigation是最常用的一种,有时为了某些效果又需要进行模态切换,即present. 我们的布局经常是在window上加一个nav ...

  9. CI框架在模型中切换读写库和读写库

    如果你想在控制器中切换在application/config/database.php中配置好的数据库group,那么你可以参考这篇博客:CI框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...

随机推荐

  1. C# NPOI 导入与导出Excel文档 兼容xlsx, xls

    之前写了个小程序,导出一些数据成Excel,程序使用的是Microsoft.Office.Interop.Excel类来操作Excel. 在本机测试的时候都好好的,但是将生成文件放到其他电脑上却怎样也 ...

  2. Android SDK更新以及ADT更新出现问题的解决办法

    http://jingyan.baidu.com/article/148a192196209d4d70c3b168.html

  3. iOS设置导航栏标题

    方法一:在UIViewController中设置self.title. 方法二:设置self.navigationItem.titleView.

  4. Daily Scrum 12.18

    对于老师课上所问为什么燃尽图(图如下)的完成小时数增加的问题,我们的理解是完成小时数是完成迭代2所需要的总共时间,当加入任务的时候,也就是蓝色部分增长的时候,完成小时数就会增加. 今日大家都在做编译实 ...

  5. vpython初探

    vpython 是python默认的3D模块,和python有一样的风格.与PyOpenGL相比,容易上手. vpython下载:vpython的官网(www.vpython.org).顺便说一句,官 ...

  6. Spark如何使用Akka实现进程、节点通信的简明介绍

    <深入理解Spark:核心思想与源码分析>一书前言的内容请看链接<深入理解SPARK:核心思想与源码分析>一书正式出版上市 <深入理解Spark:核心思想与源码分析> ...

  7. Winform中Grid跨行复制一列

    Private Sub DataCopyToGrid() '判断剪切板中是否存在内容 If Clipboard.ContainsText Then Dim str = Clipboard.GetTex ...

  8. DIV+CSS:页脚永远保持在页面底部

    页脚永远保持在页面底部 有时候,我们用CSS创建一个高度自适应布局,如何保证页脚(footer)在内容不超过一屏的情况下始终保持在布局最下方是一个比较头疼的事.我看过一些利用绝对定位的例子,但总感觉不 ...

  9. css3中的颜色

    1颜色.color:rgba(R,G,B,A) R,G,B是分别代笔红,绿,蓝值是在0到255之间的数也可以是0.0% - 100.0%,A代表的是透明度0到1之间. 2.渐变.background- ...

  10. Winodow Server Backup学习向导-window 2008

    1.安装Window Server Backup 2.备份服务器 3.恢复服务器 4.优化和备份服务器性能 Windows Server Backup 中的新增功能有哪些? Windows Serve ...