IOS 常用的界面之间的跳转总结
2018-02-07 本文已影响68人
MrLiangC
一、模态推送
模态视图之间的切换利用– presentViewController:animated:completion:
弹出视图,– dismissViewControllerAnimated:completion
返回,这是系统提供的最基本的推送方式,但是开发中常常会遇到利用模态视图进行多个页面跳转后,要返回到最初的那个界面或者发回到指定的界面,这需要了解视图控制器的两个属性presentedViewController
和presentingViewController
比如A—>B 那么A相对于B就是presentingViewController,B相对于A是presentedViewController
比如:A->b->c->d, 然后d要返回到A或者返回到指定界面,像这种多次present后,dismiss到指定界面做法如下
- 多次present后,dismiss到指定界面
- (void)disMissToViewController:(UIViewController *)viewController {
UIViewController *vc = self.presentingViewController;
NSLog(@"%@",[vc class]);
while (![vc isKindOfClass:[viewController class]]) {
NSLog(@"%@",[vc class]);
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
}
- dismiss到最底层控制器
-(void)dismissToRootViewController
{
UIViewController *vc = self;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
}
二、导航控制器
- 获取当前根视图
- (UIViewController *)presentingVC{
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIViewController *result = window.rootViewController;
while (result.presentedViewController) {
result = result.presentedViewController;
}
if ([result isKindOfClass:[UITabBarController class]]) {
result = [(UITabBarController *)result selectedViewController];
}
if ([result isKindOfClass:[UINavigationController class]]) {
result = [(UINavigationController *)result topViewController];
}
NSLog(@"%@",[result class]);
return result;
}
- push到指定控制器,
- (void)goToVC:(UIViewController *)viewController {
if (!viewController) {
return;
}
//获取当前根视图控制器
UINavigationController *nav = [self presentingVC].navigationController;
if (nav) {
[nav pushViewController:viewController animated:YES];
}
}
- pop到指定页面
- (void)popToViewController:(UIViewController *)viewController {
for (UIViewController * controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[viewController class]]) {
[self.navigationController popToViewController:controller animated:YES];
}
}
}
如果在View层想要获取view所在的当前控制器
- 获取视图所在的控制器
- (UIViewController *)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
- 获取当前显示的视图控制器,可用于接受到推送消息后,需要获取到当前正在显示的控制器进行跳转的情况
- (UIViewController *)currentViewController {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// modal展现方式的底层视图不同
// 取到第一层时,取到的是UITransitionView,通过这个view拿不到控制器
UIView *firstView = [keyWindow.subviews firstObject];
UIView *secondView = [firstView.subviews firstObject];
UIViewController *vc = [secondView viewController];
if ([vc isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)vc;
if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
return [nav.viewControllers lastObject];
} else {
return tab.selectedViewController;
}
} else if ([vc isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)vc;
return [nav.viewControllers lastObject];
} else {
return vc;
}
return nil;
}