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框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...
随机推荐
- IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法
IIS7.5上在站点下部署虚拟目录,访问虚拟目录下的项目提示与父节点配置冲突.,节点与的<system.web>节点与主站点的<system.web>冲突解决方法: 在站点下的 ...
- Gradle笔记系列(一)
1.Gradle概述 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的 ...
- oracle10g、oracle client和plsql devement 三者之间的关系
oracle10g是服务器,如果本机安装了oracle10g,没有必要安装oracle client,只要配置好DNS,就可以使用plsql devement连接 当然你也可以同时安装orac ...
- iOS 适配https
1.准备证书 首先找后台要一个证书(SSL证书,一般你跟后台说要弄https,然后让他给你个证书,他就知道了),我们需要的是.cer的证书.但是后台可能给我们的是.crt的证书. 我们需要转换一下: ...
- 20151208Study
20151208-----------------------------------------------------* Her main interest now is raising her ...
- PS:蓝天白云的制作
方法一: 1.新建(ctrl+N),根据自己的需求设置画面大小: 2.设置前景色为蓝天颜色,alt+delete,填充为天空蓝颜色: 3.滤镜-渲染-云彩,得出蓝天白云效果: 4.根据需求再调整亮度. ...
- oracle 11gr2 官方文档下载
http://www.oracle.com/technetwork/database/enterprise-edition/documentation/index.html
- 如何在win上搭建SVN服务器
本博文转自:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本, ...
- Encapsulation、Inheritance、Polymorphism
public class SoldierDemo { public static void main(String[] args) { /*Soldier test1=new Army("张 ...
- nodejs前端跨域访问
XMLHttpRequest cannot load http://localhost:3000/. No 'Access-Control-Allow-Origin' header is presen ...