【发布时间】:2017-06-28 07:42:59
【问题描述】:
按照 Apple 的文档添加和编辑信息Apple guide here 我有一个带有 tableview 的 Viewcontroller。表格视图包含一个带有“添加新”按钮的标题。如果选择了表格行,则将 detailViewController 推入堆栈。 detailViewController 也嵌入在 UINavigationController 中,就像在 Apple 的文档中一样。如果按下“Add new”,则会执行另一个 segue,以模态方式呈现 UINavigationController,进而显示 detailViewController。这工作正常,动画清楚地显示了一个模态呈现的 ViewController。
detailViewController 在 NavigationBar 中包含一个取消按钮。如果按下,则运行以下代码:
@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.
var isPresentingInAddActionMode = false
if let presentingVC = self.presentingViewController{
isPresentingInAddActionMode = presentingVC is UINavigationController
}
streekgidsModel.undoManager.endUndoGrouping()
print("undo grouping ended and undone")
streekgidsModel.undoManager.undo()
if isPresentingInAddActionMode {
dismiss(animated: true, completion: nil)
}
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}
else {
fatalError("The MealViewController is not inside a navigation controller.")
}
}
第一个 if 语句检查属性 presentingViewController 是否存在,如果存在,是否为 UINavigationController 类型。如果是这样,viewController 会以模态方式呈现,并且应该被关闭。如果不是,它会被压入堆栈,并且 owningNavigationController 应该弹出 detailViewController。 运行此代码无法按照 Apple 的描述运行。 presentingViewController 上的检查显示它存在,但类型检查返回“无效”。这被视为错误。 owningNavigationController 上的测试成功(我认为它应该失败)并且 popViewController 被执行。由于没有推送,视图控制器不会弹出或关闭,并且仍然可见。第二次按下 Cancel 会再次执行 func cancel,这会导致错误,因为在 undo manager 中不再启动一个组。
令人费解的是,我在另一个视图控制器中有相同的代码,具有相似的 UIViewTable 和导航,并且工作正常。
所以要提出这个问题:为什么这不像 Apple 描述的那样工作,为什么我的其他视图控制器按预期工作?任何意见表示赞赏。
顺便说一句,致命错误文本直接来自文档,因此命名不相关且永远不会执行。
【问题讨论】:
标签: ios swift uiviewcontroller