segmentControl实现控制器的切换
生活随笔
收集整理的這篇文章主要介紹了
segmentControl实现控制器的切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建一個空項目,定義三個控制器繼承自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]; }主控制器邏輯實現
添加子控制器
初始化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实现控制器的切换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 观阿凡达水之道之感想
- 下一篇: ubuntu 20.04 anyconn