您真的了解UIViewController跳转吗

你真的了解UIViewController跳转吗?

一:UIViewController模态跳转

//展示模态视图
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);

//关闭模态视图
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);

//只到IOS6
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 6_0);

//只到IOS6
- (void)dismissModalViewControllerAnimated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 6_0);

知识点1

a: 在官方文档中,建议这两者之间通过delegate实现交互。例如使用UIImagePickerController从系统相册选取照片或者拍照,imagePickerController和弹出它的VC之间就通过UIImagePickerControllerDelegate实现交互的。

b: 控制器的中的只读属性:presentedViewController和presentingViewController,他们分别就是被present的控制器和正在presenting的控制器。

c: Modal的效果:默认是新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止。但可以通过自定义转场来改变展现view的动画,大小,位置,是否移除跳转之前的view.这个效果可以用来模拟ipad特有的Popover弹出框。

d: 需要注意的是,默认他的实现过程是移除跳转之前的控制器的view,并将新的控制器的view展示,但跳转之前的控制器并没有被释放,而是被强引用这的。区别于导航控制器的push。

e: 通过 dismissViewControllerAnimated 来返回前一个界面的

知识点2:例如在当前A控制器利用模态跳转到另一个B控制器

1.当前A控制器,跳转代码
RecipeAddViewController *addController = [[RecipeAddViewController alloc] init]; 
addController.modalPresentationStyle = UIModalPresentationFullScreen; 
addController.transitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentViewController:addController animated:YES completion: nil]; 


2.返回当前A控制器,在刚才跳到的B控制器中,加上返回代码
[self dismissViewControllerAnimated:YES completion:NULL];

知识点3:两个重要的枚举对象

//弹出时的动画风格
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,  //从底部滑入
    UIModalTransitionStyleFlipHorizontal,  //水平翻转进入
    UIModalTransitionStyleCrossDissolve,  //交叉溶解
    UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),  //翻页
};

//弹出风格
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
        UIModalPresentationFullScreen = 0,  //代表弹出VC时,VC充满全屏
        UIModalPresentationPageSheet NS_ENUM_AVAILABLE_IOS(3_2),  //VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击.这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域;
        UIModalPresentationFormSheet NS_ENUM_AVAILABLE_IOS(3_2),  //VC的高度和宽度均会小于屏幕尺寸,VC居中显示,四周留下变暗区域;
        UIModalPresentationCurrentContext NS_ENUM_AVAILABLE_IOS(3_2),  //VC的弹出方式和弹出VC的VC的父VC的方式相同
      //自定义转场 模态转场 需要代理实现
        UIModalPresentationCustom NS_ENUM_AVAILABLE_IOS(7_0),
        UIModalPresentationOverFullScreen NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationOverCurrentContext NS_ENUM_AVAILABLE_IOS(8_0),
        UIModalPresentationPopover NS_ENUM_AVAILABLE_IOS(8_0),
        //告诉Presentation控制器忽视紧凑环境并继续使用前面的Presentation风格
        UIModalPresentationNone NS_ENUM_AVAILABLE_IOS(7_0) = -1,         
};

二:导航控制器UINavigationController跳转

//推出界面
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

//返回 将栈顶的控制器移除
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; 
 
//指定返回跳到详细的哪一个上 回到指定的子控制器
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 

//返回到最* 回到根控制器(栈底控制器)
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;

知识点1:popToViewController用法

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2]
animated:YES];

或

UIViewController *popCtl;
for (UIViewController *ctl in self.navigationController.viewControllers) {
    if ([ctl isKindOfClass:[MyViewController class]]) {
        popCtl = ctl;
        break;
    }
}
if (popCtl) {
    [self.navigationController popToViewController:popCtl animated:YES];
}

知识点2:iOS解决使用模态视图 导致无法pushViewController

模态视图默认从界面底部滑出并占据整个界面,并短暂地显示与之前不同的界面,直到用户完成某项操作。模态视图完成和程序主功能有关系的独立任务,尤其适合于主功能界面中欠缺的多级子任务。例如撰写新邮件时的模态视图.

例如:
当登录界面的作为模态视图的话. 当我们离开当前界用presentViewController弹出登录界面的话..就会导致在登录界面这个模态视图中视图间的跳转会失效. 这是由于模态视图其实是不同于导航控制器的新的视图, 并且只有将这个视图处理完成后才能回到原来的视图. 模态视图就相当于死胡同 进入就必须原路返回, 也就是不可以在模态视图中执行页面跳转.

也就是模态中无法获取导航控制器 表现在代码里则:self.navigationController是空的,哪如何让模态中的self.navigationController不空呢, 也就很简单了, 只需要将登录这个视图控制器封装成navigationController 弹出来, 而这个模态只作为这个navigationController的rootViewController即可

UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginVC];

[self.navigationController presentViewController:navi animated:YES completion:nil];

然后, 在这个模态中视图的跳转就可以有我们传过来的这个导航控制器完成了,表现在代码;则:self.navigationController是存在的. 如果再想跳转就可以用pushViewController了;因为包装了一层navigationController这个'模态'会有导航栏 自行隐藏即可

退出模态视图:

[self dismissViewControllerAnimated:YES completion:nil];

知识点3:解决使用[self.navigationController pushViewController:VC animated:YES]; push卡顿

UIViewController *vc = [UIViewController new];
[self.navigationController pushViewController:vc animated:YES];
上述代码推出界面会卡顿

解决办法:
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor 推出时你想要的控制器View的颜色]