- 模态跳转(Modal)
普通的视图控制器一般只有模态跳转的功能,这个方法是所有视图控制器对象都可以用的。
-(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
- 一般跳转前和跳转后的界面通过
delegate
进行数据交换。 - 控制器的中的只读属性:
presentedViewController
和presentingViewController
,他们分别就是被present的控制器和正在presenting的控制器。 - 通过
dismissViewControllerAnimated
来返回前一个界面的。
- 通过Segue来跳转
Segue:多出现于UIStoryboard
中,是不同类之间跳转的一根线。换种说法就是:Storyboard
上每一根用来界面跳转的线,都是一个UIStoryboardSegue
对象
跳转步骤:
- 创建一个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];