问题描述
我有 3 个 MVC,并在此处将它们命名为 A、B、C.
I have 3 MVCs and name them A, B, C here.
A 是主 MVC,有一个按钮Menu"通过 popover segue 连接到 B.
A is the main MVC with a button "Menu" connected to B with popover segue.
A 也通过手动显示转场连接到 C.
A also connected to C with a manual show segue.
B 是一个 popover MVC,带有一个Detail"按钮,通过 unwind segue 连接到 A.
B is a popover MVC with a button "Detail" connected to A with unwind segue.
C 是具有详细信息的详细 MVC.
C is the detail MVC with detail info.
在 A 的 unwind 函数内部,我调用 performSegueWithIdentifier 来显示 C.
Inside the unwind function of A. I call performSegueWithIdentifier to show C.
预期行为是
- 点击B中的详细信息"按钮
- B消失,A出现
- C 出现
但是运行我得到的应用程序.
But running the app I got.
- 点击B中的详细信息"按钮
- B消失,A出现
- C出现
- C消失而A出现
C 突然出现又消失,这不是我想要的.
C show up and disappear suddenly which is not what I want.
一些额外的信息
- 更多按钮需要弹出框 B.
- A 嵌入在 UINavigationController 中.连接 A -> C 而不是 B -> C,用于 C 顶部的后退按钮.
推荐答案
似乎 unwind 函数不是调用 performSegueWithIdentifier 的正确位置.
Seems unwind function is not the correct place to call performSegueWithIdentifier.
UIKit 将在调用 unwind 函数后弹出视图控制器.
UIKit will pop view controller after calling the unwind function.
因此在 unwind 函数中推送新的视图控制器会快速弹出.
Thus push new view controller inside unwind function will pop quickly.
解决办法是延迟 performSegueWithIdentifier.
The solution is to delay performSegueWithIdentifier.
解决方案 1:不能正常工作
添加一个 bool 实例并在 viewDidAppear 内部使用此实例来确定我们是否执行 segue.
Adding an bool instance and inside viewDidAppear use this instance to determine if we perform segue.
如果 B 控制器是弹出框,则不起作用.B弹出框消失后,A控制器不会调用viewDidAppear.
Won't work if B controller is a popover. After B popover disappear, viewDidAppear is not called for A controller.
解决方案 2:工作
将 performSegueWithIdentifier 推入主队列.
Push performSegueWithIdentifier into main queue.
@IBAction func unwind(segue : UIStoryboardSegue) {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("SomeSegue", sender: self)
}
}
这篇关于如何在 unwind 函数中执行 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!