解雇两个控制器Swift [英] dismiss two controllers Swift
问题描述
我有这种情况:
我有一个第一个视图控制器,当点击按钮时我在模态模式下打开另一个视图控制器,在此视图控制器中我点击另一个按钮,我在模态视图中打开另一个视图控制器,在其中有一个按钮,当我点击它时,我想去第一个视图控制器而不重新初始化它。
我该怎么办?
I have a first view controller , when tap on button in it I open in modal mode another view controller , in this view controller when I tap another button I open in modal view another view controller and in it there is a button and when I tap on it I want to go to first view controller without re-initialize it. How do I do it?
推荐答案
这是 unwind segue的完美情况。
将它放在你的第一个viewController(你想要返回的那个)中:
Put this in your first viewController (the one you want to return to):
@IBAction func backFromVC3(_ segue: UIStoryboardSegue) {
print("We are back in VC1!")
}
然后在第3个viewController的Storyboard中, control -drag从你的按钮到viewController顶部的退出图标,从弹出窗口中选择 backFromVC3
。
Then in the Storyboard in your 3rd viewController, control-drag from your button to the exit icon at the top of the viewController and choose backFromVC3
from the pop-up.
现在,当用户按下VC3中的按钮时,VC3和VC2将被解雇,你将返回VC1。
Now, when the user presses the button in VC3, both VC3 and VC2 will be dismissed and you will return to VC1.
如果你没有使用Storyboard,你可以解雇viewControllers码。下面是一个按钮处理程序的代码,用于解除两个级别的viewController:
If you are not using Storyboards, you can dismiss the viewControllers with code. Here is code for a button's handler to dismiss two levels of viewController:
func doDismiss(_ sender: UIButton) {
// Use presentingViewController twice to go back two levels and call
// dismissViewController to dismiss both viewControllers.
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}
这篇关于解雇两个控制器Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!