更新时间:2023-09-02 17:50:10
这是 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)
}