码迷,mamicode.com
首页 > 其他好文 > 详细

segmentControl实现控制器的切换

时间:2016-09-02 11:26:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

- (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实现控制器的切换

标签:

原文地址:http://www.cnblogs.com/striveLD/p/5832527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!