美文网首页
IOS多控制器之间的跳转方式

IOS多控制器之间的跳转方式

作者: 香菜那么好吃为什么不吃香菜 | 来源:发表于2020-07-27 17:09 被阅读0次

你见过一个app只在一个界面就实现了你所有想要的嘛?你没有对不对!所以我们需要在各个界面之间来回跳转,每个界面的对应着不同的需求,在跳转中实现你想要的不同需求,实现控制器之间的跳转有三种方式~

UINavigationController

实现方式:使用UINavigationController主要是通过pop、Push动作来实现的。首先需要设置根控制器,之后的跳转动作都是根据控制器来完成跳转。
同时,UINavigationController的外观只需要设置一次,之后派生的导航栏都会保持根控制器的外观。设置UINavigationController的外观需要在initialize方法中设置外观代理对象(appearance)来完成设置,注意initialize是类方法。

//设置根控制器
let root = UINavigationController(rootViewController:viewController())
//使用UINavigationController时使用push和pop来实现跳转
//跳转到指定控制器 Pushes a view controller onto the receiver’s stack
[UINavigationController pushViewController: 目标控制器  animated:true/false];
//返回上一个控制器 Pops the top view controller from the navigation stack
[UINavigationController popViewControllerAnimated: true/false];
//返回到根控制器 Pops all the view controllers on the stack except the root view controller
[UINavigationController popToRootViewControllerAnimated:true/false];
//返回到指定控制器 Pops view controllers until the specified view controller is at the top of the navigation stack
[UINavigationController popToViewController: 已经存在的目标控制器 animated:true/false];
//侧滑手势返回上一个控制器
self.navigationController.interactivePopGestureRecognizer
push
Objects managed by the navigation controller

UITabBarController

实现方式在使用UITabBarController时,要把所有要跳转的控制器都一次性设置完成,自控制器是同时存在的,在跳转目标子控制器的时候不会释放其他的子控制器。

代码实现

使用addChildViewController

这种方法每次只能设置一个子控制器。

[self addChildViewController:ViewController1()];
[self addChildViewController:ViewController2()];
[self addChildViewController:ViewController3()];
使用viewControllers

要设置多个子控制器的时候可以使用viewControllers来设置多个子控制器。
viewControllers是一个数组,使用时,创建完控制器之后把控制器放到数组里面。要注意数组中的顺序,就是tabBar展示控制器的顺序。

self.virwControllers = [ViewController1,viewController2,viewController3];

UIPresentationController

实现方式使用presentViewController可以跳转任意控制器,但在跳转完成之后,如果要返回就要完成dismiss方法。presentViewController跳转的特点是从屏幕下方推出知道覆盖整个完整屏幕。
要注意的是,在完成dismiss方法之后,presentViewController会从内存中释放。

[self presentViewController:vc animated:YES completion:^{
         
}];

· completion是完成跳转之后执行的回调,我们一般填nil。
· 在跳转完成之后一定要首先写dismiss方法
dismissViewControllerAnimated(true/false, completion:nil)
·这样就完成了返回,同时该控制器也会从控制器中释放。

[ self presentViewController:SVC animated: YES completion:nil];
[ self dismissViewControllerAnimated: YES completion: nil ];
presentedViewController和presentingViewController

假设从A控制器通过present方式跳转到B控制器,那么A.presentedViewController就是B的控制器;B.presentingViewController就是A控制器。

dismissViewControllerAnimated:方法的使用

假设从A控制器跳转到B控制器,现在想要回到A控制器,那么就需要A控制器调用

注意: 是想要从B控制器回到A控制器时,需要A控制器调用上面的方法,而不是B控制器。其实就是,你想要回到哪个控制器,哪个控制器就得调用dismissViewControllerAnimated:这个方法。

举例来说,从A控制器跳转到B控制器,在B控制器中点击了返回按钮,期望能够回到A控制器,则B控制器中点击返回按钮触发事件的代码是:

[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
         
}];

注意:这段代码是在B中执行,因此 self.presentingViewController 实际上就是A控制器,这样就返回到了A控制器。
如果多个控制器都通过 present 的方式跳转呢?比如从A跳转到B,从B跳转到C,从C跳转到D,如何由D直接返回到A呢?可以通过 presentingViewController 一直找到A控制器,然后调用A控制器的 dismissViewControllerAnimated 方法。方法如下:

UIViewController *controller = self;
while(controller.presentingViewController != nil){
    controller = controller.presentingViewController;
}
[controller dismissViewControllerAnimated:YES completion:nil];

PS:如果不是想直接返回到A控制器,比如想回到B控制器,while循环的终止条件可以通过控制器的类来判断。

相关文章

网友评论

      本文标题:IOS多控制器之间的跳转方式

      本文链接:https://www.haomeiwen.com/subject/eawfrktx.html