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
原创粉丝点击