我的代码有效,但我试图理解它。它是用于管理"取消"的一些代码。可以从"显示" segue(当用户点击表格视图单元格进行编辑时显示)或者#34;呈现模态" segue(当用户单击" +" bar按钮项以向表视图添加新单元格时)。下图。我对UINavigationController和navigationController属性感到困惑。如果我错过了一些非常明显的东西,请道歉。
// Apple says below is nil if neither the current view controller nor any of its ancestors were presented modally
let isPresentingInAddMode = presentingViewController is UINavigationController
if isPresentingInAddMode {
// Modal segues need to be dismissed
dismiss(animated: true, completion: nil)
} else {
// But Show segues are "popped" off of a stack of controllers.
navigationController!.popViewController(animated: true)
}
这是我不理解的地方: - 如果我通过" Show"来到了详细视图控制器segue,执行期间暂停,以及选项 - 单击navigationController,Xcode说它是UINavigationController?如果我暂停执行其他条件&使用调试器: po navigationController! ==无 - Xcode称它的计算结果为false,因此navigationController是一个有效的UINavigationController。
那为什么不
presentingViewController is UINavigationController
当我使用"显示"?时,在最顶层的陈述中等于true 也许我不理解"现在"。呈现的东西只发生在模态segue上,所以没有presentViewController吗? 如果我回顾一下我的故事板(见下图),详细视图控制器的一个祖先是一个表视图控制器,其中嵌入了一个导航控制器,给出了Apple对presentationController的定义,不应该这样做: presentsViewController是UINavigationController 在这种情况下也是如此吗?
并且会:
presentingViewController != nil
实现相同的结果还是有一个重要的理由来验证presentViewController是否是UINavigationController?
非常感谢有人帮助我解析这个问题。 约翰
" ShowDetail" segue是一个" Show" segue源自表格视图单元格。 " AddItem" segue是善良的"现在的模式"并且源自" +"添加栏按钮项。
可能没有必要在表格视图控制器中查看segue代码的准备工作,但如果您有点好奇:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "ShowDetail" { // is this the "ShowDetail" segue? and if it is...
// ... get the IndexPath for the row the user clicked on (the selected row)
let indexPath = tableView.indexPathForSelectedRow!
let destinationViewController = segue.destination as! DetailViewController // downcast the destination as the specific class DetailViewController
// Get the to do item that the user clicked on
let selectedToDo = toDoArray[indexPath.row]
// Pass selectedToDo to the toDoItem variable in our destinationViewController
destinationViewController.toDoItem = selectedToDo
}
}
答案 0 :(得分:2)
也许我不理解“现在”。
也许。我们来参考图表:
有两种常用的内置segue:
显示,以前称为Push。如果在UINavigationController情况下按预期使用,则调用pushViewController
。推送的视图控制器具有navigationController
。通过调用popViewController
来执行返回。
现在,以前称为莫代尔。致电presentViewController
(现称为present
)。呈现的视图控制器具有presentingViewController
。通过调用dismiss
。
但是,有一个复杂的问题会让你感到困惑:如果在UINavigationController情况下使用而不是,而不是宇宙按预期爆炸,它会神奇地将自己变成{{1 }}