【问题标题】:Swift difficulty understanding UINavigationController & navigationController re: presentingViewControllerSwift 难以理解 UINavigationController & navigationController re:presentingViewController
【发布时间】: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


【解决方案1】:

也许我不理解“现在”。

也许吧。我们来看看图表:

常用的内置segue有两种:

  • 显示,以前称为推送。如果在 UINavigationController 情况下使用,按预期调用pushViewController。推送的视图控制器有一个navigationController。通过调用popViewController 执行返回。

  • Present,以前称为 Modal。致电presentViewController(现在称为present)。呈现的视图控制器有一个presentingViewController。调用dismiss进行返回。

但是,有一个复杂的情况可能会让您感到困惑:如果在 UINavigationController 情况下使用show,而不是宇宙按预期爆炸,它会神奇地将自己变成present

【讨论】:

  • 不幸的是,人们在非正式场合经常说“现在”,而他们的意思是“推动”,所以你必须小心。
  • 这太棒了!很有帮助。所以modals总是有一个presentingViewController。使用这两个语句中的任何一个来测试视图控制器是否是模态的是否合适? “presentingViewController != nil”还是“presentingViewController 是 UINavigationController”?
  • 差不多。仔细看我说的话。询问self.presentingViewController != nil 是否是如何得知self 是否是呈现的视图控制器。询问self.navigationController != nil 是否是如何得知self 是否为推送视图控制器。 — 但是,一般来说,您永远不会知道这一点,因此在实践中通常不会出现这个问题。
  • 再次感谢。这种代码混乱来自我尝试使用 Apple'sFoodTracker 应用程序学习 Swift。看起来 ShowDetail 错误名称是从库比蒂诺那里学来的 :) developer.apple.com/library/content/referencelibrary/…
  • 没有。更仔细地查看屏幕截图。 "ShowDetail" 在这里只是一个任意字符串:segue 的字符串 identifier。也可以是"RevealTheDetailsToMe"。 segue 本身 是一个 Show segue,正如您可以从下面的 Kind 弹出窗口中看到的那样。
猜你喜欢
  • 2010-10-28
  • 2014-11-15
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多