【发布时间】:2017-01-28 15:29:19
【问题描述】:
我的代码有效,但我正在努力理解它。这是一些用于管理“取消”按钮的代码,该按钮可以从“显示”segue(当用户单击表格视图单元格进行编辑时呈现)或“呈现模态”segue(当用户单击“ +" 条形按钮项以将新单元格添加到表格视图中)。下图。我对 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)
}
以下是我不理解的内容: - 如果我通过“显示”segue 到达详细视图控制器,在执行期间暂停,然后选择单击导航控制器,Xcode 说它是 UINavigationController?如果我在 else 条件中暂停执行并使用调试器: po导航控制器! == 无 - Xcode 说它评估为假,所以 navigationController 是一个有效的 UINavigationController。
为什么不呢
presentingViewController is UINavigationController
当我使用“显示”呈现时,在最上面的语句中等于 true?
也许我不理解“现在”。正在呈现仅在模态序列中发生的东西,所以没有呈现视图控制器?
如果我回头看我的故事板(见下图),详细视图控制器的祖先之一是一个表格视图控制器,它嵌入了一个导航控制器,因为苹果对presentingViewController 的定义不应该:
presentingViewController 是 UINavigationController
在这种情况下也是如此吗?
并且会:
presentingViewController != nil
达到同样的结果还是有一个重要的理由来验证presentingViewController是一个UINavigationController?
非常感谢任何好心的人帮助我解析这个问题。 约翰
“ShowDetail”转场是源自表格视图单元格的“Show”转场。 “AddItem”segue 是一种“Present Modally”,源自“+”添加栏按钮项。
可能没有必要在 table view controller 中查看 prepare for 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
}
}
【问题讨论】:
-
请在调用 ViewControllers 的地方添加代码
-
将镜头代码 sn-ps 放在两个此字符之间:`(反引号)
-
嗨 - 有一个 Show segue - “ShowDetail” 是它的标识符。从“+”添加栏按钮项调用“AddItem”,而从表格视图单元格调用“ShowDetail”。我将为上面的表格视图控制器添加“prepare(for segue:) 代码,但这并不重要:了解 UINavigationController 与 navigationController,应该吗?谢谢
-
是的 - 我选择的标识符名称很糟糕,但“ShowDetail”是标识符。转场是种类:属性是“显示(例如推送)”。
标签: swift uinavigationcontroller navigationcontroller