[英]How to dismiss previous and current viewcontroller by click done button using Swift
I have three view controllers (VC1, VC2, VC3)
. 我有三个视图控制器
(VC1, VC2, VC3)
。 Here, VC1 add button click to present VC2 then VC2 I have another one add button for make a VC3 present. 在这里,单击VC1添加按钮以显示VC2,然后单击VC2,我还有另一个添加按钮用于显示VC3。 VC3 I have navigation bar
cancel
and done
button. VC3我有导航栏“
cancel
和“ done
按钮。 If I click I can able to dismiss and show VC2 but If I click done button I need to show VC1 (Between VC1 and VC3 need to dismiss VC2). 如果单击“我可以关闭并显示VC2”,但是如果单击“完成”按钮,则需要显示VC1(在VC1和VC3之间需要关闭VC2)。 How to achieve this?
如何实现呢?
I am using below code for presenting and dismissing 我正在使用以下代码进行演示和解散
VC1 VC1
@IBAction func presentFirst(_ sender: Any) {
let firstvc = self.storyboard?.instantiateViewController(withIdentifier: "firstcontroller") as! FirstViewController
let navigationController = UINavigationController(rootViewController: firstvc)
self.present(navigationController, animated: true, completion: nil)
}
VC2 VC2
@IBAction func presentSecond(_ sender: Any) {
let secondtvc = self.storyboard?.instantiateViewController(withIdentifier: "secondcontroller") as! SecondViewController
let navigationController = UINavigationController(rootViewController: secondtvc)
self.present(navigationController, animated: true, completion: nil)
}
@IBAction func doneAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func cancelAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
VC3 VC3
@IBAction func doneAction(_ sender: Any) {
// Need to dismiss current and previous VC2
}
@IBAction func cancelAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
A good practice would be to use prepare(for segue:
at VC2 in order to send a reference of its self to VC3 so you can dismiss it later. 良好的做法是在VC2上使用
prepare(for segue:
:),以便将其自身的引用发送到VC3,以便以后可以将其关闭。
So first of all add a reference variable in VC3 所以首先在VC3中添加一个参考变量
var vc2Ref: VC2!
Then in VC2 you can set this variable's value like this 然后在VC2中,您可以像这样设置此变量的值
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let vc3 = segue.destination as? VC3 { vc3.vc2Ref = self } }
And now you are able to dismiss VC2 and VC3 现在您可以关闭VC2和VC3
@IBAction func doneAction(_ sender: Any) { //dismiss current VC3 self.dismiss(animated: true, completion: nil) //dismiss previous VC2 self.vc2Ref.dismiss(animated: true, completion: nil) }
About dismiss() method document . 关于dismiss()方法文档 。
Discussion
讨论区
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
如果您连续显示几个视图控制器,从而构建了一个显示的视图控制器堆栈,则在堆栈中较低的视图控制器上调用此方法将取消其直接子视图控制器以及该堆栈上该子视图之上的所有视图控制器。 When this happens, only the top-most view is dismissed in an animated fashion;
发生这种情况时,只有最上面的视图会以动画方式关闭; any intermediate view controllers are simply removed from the stack.
只需从堆栈中删除所有中间视图控制器即可。 The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
最顶层的视图使用其模式过渡样式来消除,该样式过渡样式可能与堆栈中较低的其他视图控制器使用的样式不同。
In brief, if a presented stack is as following, 简而言之,如果显示的堆栈如下所示,
A -> B -> C -> D -> E
// A.present(B), then B.present(C), ... , D.present(E)
// E is top-most view controller.
If call E.dismiss()
, then the stack will be 如果调用
E.dismiss()
,则堆栈将是
A -> B -> C -> D
Then, if call C.dismiss()
, then stack will be 然后,如果调用
C.dismiss()
,则堆栈将为
A -> B
// NOTE:
// Don't call `E.dismiss()`, `D.dismiss()`, `C.dismiss()` in sequence.
// ONLY call `C.dismiss()`. Just as the `Discussion` said.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.