- 模態跳轉(Modal)
普通的視圖控制器一般只有模態跳轉的功能,這個方法是所有視圖控制器對象都可以用的。
-(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
- 一般跳轉前和跳轉后的界面通過
delegate
進行數據交換。 - 控制器的中的只讀屬性:
presentedViewController
和presentingViewController
,他們分別就是被present的控制器和正在presenting的控制器。 - 通過
dismissViewControllerAnimated
來返回前一個界面的。
- 通過Segue來跳轉
Segue:多出現于UIStoryboard
中,是不同類之間跳轉的一根線。換種說法就是:Storyboard
上每一根用來界面跳轉的線,都是一個UIStoryboardSegue
對象
Segue
跳轉步驟:
- 創建一個UIStoryboardSegue對象
+(instancetype)segueWithIdentifier:(nullable NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination performHandler:(void (^)(void))performHandler NS_AVAILABLE_IOS(6_0);
or
-(instancetype)initWithIdentifier:(nullable NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination NS_DESIGNATED_INITIALIZER;
- 調用
-(void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
- 系統在跳轉前自動調用
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
segue.sourceViewController;
segue.destinationViewController;
}
- 通過NavigationController跳轉
[self pushViewController:.. animated:YES];
[self hidesBottomBarWhenPushed];
- UITabBarController
self.selectedItem = 1;
如何從Storyboard中創建ViewController
//通過使用storyborardID去獲取啟動頁viewcontroller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Launch Screen" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"LaunchScreen"];
UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
加載啟動圖
//主界面加載 顯示之前
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self launchAnimation];
}
切換rootViewController
[UIView transitionWithView:weakSelf.window duration:0.8 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
BOOL oldState = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:NO];
[weakSelf.window setRootViewController:nav];
[UIView setAnimationsEnabled:oldState];
} completion:^(BOOL finished) {
}];
跳轉時的動畫效果
CATransition *animation = [CATransition animation]; [animation setDuration:3.3]; [animation setType:kCATransitionFade]; //淡入淡出 [animation setSubtype:kCATransitionFromLeft]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; MapViewController *mapview = [[MapViewController alloc] initWithNibName:Nil bundle:NULL]; [self.navigationController pushViewController:mapview animated:NO]; [self.navigationController.view.layer addAnimation:animation forKey:nil];