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框架在控制器中切换读写库和读写库 如果你是希望在模型中切换 ...
随机推荐
- Android开发学习---使用Intelij idea 13.1 进行android 开发
1.为什么放弃eclipse?太卡!! 实在受不了eclipse的卡了,运行WEB项目还好,但android开发实在太慢,太慢!经常卡死,CPU经常被占满! 看网上很多人都说比Intelij idea ...
- 序列化--dict与(file)文件读写
在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: d = dict(name='Bob', age=20, score=88) 可以随时修改变量,比如把name改成'Bill',但 ...
- #include <cstdio>
#include <cstdio> using namespace std; int main() { int gx; gx=6; printf("%d\n",gx); ...
- VS2012开发工具BUG
发现VS2012开发工具 新建立项目名称长度限制的一个bug, 向导方式名称长度限制56个,但建立好的项目名称长度是可以改的很长很长. 也有想是不是生成程序集的名称会有限制,同样这里生成程序集名称,及 ...
- System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception
[15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...
- Loadrunner,将http请求返回的中文结果打印出来
Loadrunner 做保险承保业务测试 1. 保险正常业务流程:保费计算--->保存--->申请核保--->核保--->缴费(出保单) 问题描述: 脚本录制,参数化完成后,R ...
- mysql分页原理和高效率的mysql分页查询语句
该博来自网络转载!!!供自己学习使用!!! 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我们又该怎 ...
- windows读取mac格式移动硬盘的方法
本文记录了一些window与mac数据在移动设备上互相拷贝的经验. 一.准备 家里有一台mac电脑,限于硬盘空间比较小,需要定期备份一些数据. 由于备份数据大小在20G左右,并且并没有压缩为一个压缩文 ...
- datagridview 定位到最后一行,定位不准的原因
C# 写的 winform 程序,定位到最后一行 dgvGoods.FirstDisplayedScrollingRowIndex = dgvGoods.Rows.Count - 1; 测试中发现,一 ...
- fatal error: call to undefined function imagettftext
参照:http://stackoverflow.com/questions/7290958/php-fatal-error-call-to-undefined-function-imagettftex ...