【问题标题】:How can a PresentingViewController get notified that its PresentedViewController dismissed itself?PresentingViewController 如何获得通知其 PresentedViewController 自行解散?
【发布时间】:2018-05-01 05:38:00
【问题描述】:

鉴于:

  • 呈现 ViewController B 的 ViewController A
  • ViewController B 没有对 ViewController A 的引用(隐含的 presentingViewController 属性除外)
  • ViewController B 对自己调用 dismiss 并且什么都不做

我想要达到的目标:

  • ViewController A 想知道 ViewController B 何时被解雇以清理某些状态

限制:

  • 我不想使用 KVO
  • 我不想以任何方式修改 ViewController B 或其行为

到目前为止我发现了什么:

  • dismiss(animated:completion:)according to the documentation 将呼叫转发到其presentingViewController。但似乎dismiss(animated:completion:)调用,而是一个私有方法_performCoordinatedPresentOrDismiss:animated:
  • iOS documentation on presentingViewController 具有误导性。它说“呈现的视图控制器将此属性设置为呈现它的视图控制器”,但事实并非如此。在 iOS 11 中,这将始终指向调用 present 的 VC 的根父 VC。同样,documentation on presentedViewController 具有误导性。它说“调用该方法的视图控制器将此属性设置为它呈现的视图控制器”,这不是全部。调用 present 的 VC(其所有父 VC 和子 VC)层次结构中的每个 VC 都将指向相同的 presentedViewController

【问题讨论】:

  • 可能在 ViewController B 的 viewWillDisappearviewDidDisappear 中发送通知?
  • 不幸的是我无法修改 B。我可以将它包装在 containerVC 中,如下面的答案中所述,但也不是很好
  • 或者子类化它?
  • 没错,这也是可能的

标签: ios uiviewcontroller coordinator-pattern


【解决方案1】:

在您的控制器 A 中,将其命名为 UINavigationControllerDelegate 并使用 navigationController:didShowViewController 标记控制器 B (isControllerBisPresented = true) 的演示文稿。当 viewDidAppear 的 B 时,检查 isControllerBisPresented 是否为真。

【讨论】:

  • ViewController A 和 B 都不是 UINavigationController,所以很遗憾这没有多大帮助
  • 你有 TabViewController 吗,你能解释一下你的架构吗
  • ViewController A 和 B 都是普通的 UIViewController 子类。我无法修改 B。A 通过调用 present 呈现 B。
  • 在 AppDelegate 中维护一个标志并在 viewDid 方法中检查它。虽然它的小手册
  • 是的,很漂亮的手册:-/ 如果我展示一些本身已经展示的东西,也不是很可组合
【解决方案2】:

一个丑陋的解决方法是使用在deinit 中执行某些操作的中间人。所以A 呈现M 嵌入B 作为childVC。当B 自行解除时,M 也将被隐式解除,因此 应该 调用 deinit 方法。在那里它可以通知A它已被解雇。

这是脆弱的,因为某些引用循环可能会阻止 M 被释放,这将导致 A 没有得到通知。所以我宁愿找到更好的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多