segmentControl实现控制器的切换
来源:互联网 发布:广告图片制作软件 编辑:程序博客网 时间:2024/05/18 02:49
创建一个空项目,定义三个控制器继承自UIViewController的子控制器,其中一个为主控制器,分别管理另外两个控制器。
主控制器:XCMainController
第一个控制器:XCFristController
第二个控制器:XCSecondController
在AppDelegate的- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions方法中定义主窗口
- (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; } }];}
2 0
- segmentControl实现控制器的切换
- iOS: SegmentControl 实现页面切换的四种方法
- iOS: SegmentControl 实现页面切换的四种方法
- iOS: SegmentControl 实现页面切换的四种方法
- 第三方SegmentControl+fragment实现选择切换
- SegmentControl的实现与使用
- SegmentControl的实现与使用
- SegmentControl点击切换View
- SegmentControl点击切换View
- 自定义导航控制器的titleView实现多控制器的切换
- 导航控制器实现视图的分层切换
- 利用视错觉原理实现一个自定义的SegmentControl
- SegmentControl的基本设置
- SegmentControl的基本设置
- 视图控制器的切换
- segmentcontrol
- SegMentControl
- SegmentControl
- 程序员的故事 009 在小公司工作与在大公司工作的区别
- 【转】CSS Margin Border Padding 详解
- SPOJ Use of Function Arctan
- 修改Sublime Text 3 的侧边栏字体大小,字体类型
- Android实现类淘宝多图评价
- segmentControl实现控制器的切换
- 2016年元月份腾讯测试开发补录面试总结
- 自定义AsyncTaskLoader
- UESTC 1063 秋实大哥与妹纸 二叉堆(大根树)
- Android开发之发送自定义广播
- DB2 基础系列文章
- 杭电1161Eddy's mistakes
- servlet处理客户请求,MVC设计模式
- 定位权限是否开启