【发布时间】:2018-04-12 06:30:39
【问题描述】:
我正在学习来自 Apple Developer 的教程:Start Developing iOS Apps (Swift),我对推送和模态搜索感到困惑。
有两个场景,导航栏的保存和取消按钮,从场景2回到场景1。
如果取消按钮被按下,它将调用不同的方法来关闭场景 2:
@IBAction func cancel(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
let isPresentingInAddMealMode = presentingViewController is UINavigationController
if isPresentingInAddMealMode {
dismiss(animated: true, completion: nil)
}
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}
else {
fatalError("The MealViewController is not inside a navigation controller.")
}
}
在这个方法中,如果场景是通过modal segue呈现的,就调用dismiss(animated:completion:),并且 如果场景是通过 push segue 呈现的,则调用 popViewController(animated:) 从导航堆栈中弹出 ViewController。
但是对于 Save 按钮,本教程重写了场景 2 中的方法 prepare(for:sender:) 和场景 1 中的操作方法 unwindToMealList(sender:)。 并将保存按钮拖到退出(场景停靠中的按钮)并选择 unWindToMealList(sender:) 方法。 所以流程将是:prepare(for:sender:) -> 场景 2 被取消,场景 1 呈现 -> unWindToMealList(sender:)
我想知道当按下 Save 按钮时,代码 sn-ps 并没有明确地关闭场景 2 并删除导航堆栈中的 ViewController。 我知道模态 segue 不会将 ViewController 推送到导航堆栈,但 push segue 会推送它。 为什么代码 sn-ps 没有从导航栈中弹出?
非常感谢。
【问题讨论】:
标签: ios iphone swift uiviewcontroller