【问题标题】:How to get exact PresentingViewController when Modal View dismissed模态视图关闭时如何获得准确的 PresentingViewController
【发布时间】:2016-04-11 07:53:34
【问题描述】:

我的应用导航如下:

NavigationController ---> RootViewController --(Show Segue)-> SomeViewController --(Show Segue)-> ParentViewController (With ContainerView)

所以,ParentViewController 有一个容器视图。 此容器视图在运行时以编程方式填充,具体取决于用户选择。 所以,基本上视图层次结构是这样的:

ParentViewController (With ContainerView) ---(Embed Segue) --> ContainerViewController --(Custom Segue,用于决定在运行时显示哪个子节点) ---> FirstChildViewController/SecondChildViewController

现在,当在 SecondChildViewController 中点击一个按钮时,我会显示一个模式视图。到目前为止一切都很好。

但是,现在我想在关闭 ModalViewController 时更新 SecondChildViewController 中的数据。我尝试在 ModalViewController 中这样做:

SecondChildViewController *secondChildVC = (SecondChildViewController *)self.presentingViewController;
[self dismissViewControllerAnimated:YES completion: ^{ [secondChildVC updateList];   }];

但是,我收到以下错误:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[UINavigationController updateList]:发送到实例的无法识别的选择器 0x127e451b0'

我该如何解决这个问题?那么,具体来说,我怎样才能得到“真实的”presentingViewController?我知道它的导航有点奇怪,而且从用户体验的角度来看,视图堆叠太多,但这是客户想要实现的方式。

【问题讨论】:

    标签: ios objective-c iphone segue uistoryboard


    【解决方案1】:

    我不确定我是否正确理解了您的问题。如果您有一个 ViewController A,呈现 ViewController B,并且如果您希望在 B 被解除时调用 A 中的方法 - 您可以让 A 成为 B 的委托。

    在此处阅读有关协议和委托的更多信息: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

    【讨论】:

    • 抱歉有任何混淆,但您几乎是正确的。我知道我总是可以选择协议/委托机制。但是,这个问题并不少见。而不是 SecondChildViewController 实例,我得到 UINavigationViewController 实例,作为presentingViewController。有没有办法找出“真正的”presentingViewController?谢谢你的回复!!
    • 试试 UINavigationViewController.topViewController
    • 不...这确实会给我 ParentViewController 并且必须再次寻找它的孩子。到目前为止,我已经实现了委托模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多