iOS学习之模态Model视图跳转和Push视图跳转的混合需求实现原理

在研发中总会遇到一些莫名的需求,本着存在即合理的态度跟大家分享一下"模态Model视图跳转和Push视图跳转的需求实现".

1.连续两次模态Model视图之后,然后返回首页(A -> B -> C -> A)

①效果图展示:

776982-20161226155053773-531129180.gif

②实现思想解读:

一开始大家的思维肯定是一层一层的推出控制器,对这是最直接的办法,但是Apple的工程师思维非同凡响,其实你只需要解散一个Modal View Controller就可以了;即处于最底层的View Controller,这样处于这个层之上的ModalView Controller统统会被解散;那么问题在于你如何获取最底层的View Controller,如果是iOS4.0,你可以使用parentViewController来获得当前Modal ViewController的“父View Controller”并解散自己;如果是iOS 5,你就得用presentingViewController了;

③核心代码展示:

/** 在C页面的DisMiss方法里面添加一下代码(iOS5.0) */
if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];
} else {
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
}
    
/** 在C页面的DisMiss方法里面添加一下代码(iOS6.0+) */
if ([self respondsToSelector:@selector(presentingViewController)]){
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
else {
    [self.parentViewController.parentViewController dismissViewControllerAnimated:YES completion:nil];
}

2.在模态Model推出的视图中Push下一个带导航栏的视图,然后返回首页(A -> B ->C -> A)

①效果图展示:

776982-20161226155333867-2139190975.gif

②实现思想解读:

如果没有UINavigationController导航栏页面之间切换是不能实现Push操作的,那我们平时见得无导航栏Push到下一页是怎么实现的呢? 现在跟大家分享一下实现原理, 就是在第一次Model出来的控制器提前包装一个导航栏,并在Model出来控制器实现UINavigationController的代理方法,UINavigationControllerDelegate判断当前Model出来的控制器是否为自身控制器,这样做的目的就是为了更安全的隐藏该隐藏的控制器导航栏;虽然导航栏隐藏了,但是作为导航栏的属性还是存在的,所以我们现在就可以不知不觉得在Model出来的控制器里面Push出下一个页面,而且下一个页面还是带导航栏的,这样Push出来的控制器,不仅没有消失原有的Pop功能操作,而且还可以实现DisMiss操作;

③核心代码展示:

/** 这里用到的核心处理办法是 */
/** 1.在A控制器模态Model推出B控制器的时候先给B控制器包装一个导航控制器 */
UINavigationController *ANavigationController = [[UINavigationController alloc] initWithRootViewController:[[BViewController alloc] init]];
[self presentViewController:ANavigationController animated:YES completion:nil];

/** 2.在B控制器遵守UINavigationControllerDelegate实现代理协议,隐藏当前控制器的导航栏 */
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自身控制器
    BOOL isShowMyController = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowMyController animated:YES];
}
#pragma mark - Push出C控制器
[self.navigationController pushViewController:[[CViewController alloc] init] animated:YES];

/** 3.在C控制器里面可直接在返回按钮方法里DisMiss */
[self.navigationController dismissViewControllerAnimated:YES completion:nil];

感谢:
https://www.cnblogs.com/dingding3w/p/6222626.html

你可能感兴趣的:(iOS学习之模态Model视图跳转和Push视图跳转的混合需求实现原理)