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; } }]; }

转载于:https://www.cnblogs.com/striveLD/p/5832527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值