将视图控制器的父级推送到具有多个 UINavigationController 的 UITabBarController 中的 UINavigationController 堆栈上
所以这是一个我真正遇到麻烦的问题。我使用此代码来检索被推送到 UINavigationController 堆栈的 UIViewController 的父控制器:
MyAppDelegate *delegate = [[UIApplicationDelegate sharedApplication] delegate];
UINavigationController *nav = delegate.navigationController;
MyParentViewController *parent = (MyParentViewController *)nav.topViewController;
parent.myProperty = @"propertyValue";
但是,这似乎仅在您使用具有单个导航控制器的应用程序时才有效。我的结构是:
->UITabBarController
-->UINavigationController
--->MyYetAnotherParentViewController
-->UINavigationController
--->MyOtherParentViewController
-->UINavigationController
--->MyParentViewController
这意味着我在标签栏控制器中有 3 个导航控制器。
我目前位于第三个导航控制器中,并在 MyParentViewController 上方推送了一个视图控制器。
我正在尝试使用属性将数据从我推送到 MyParentViewController 的 UIViewController 传递。如果我有这个设置,我将如何检索我推送到堆栈的 UIViewController 的父级?
So here's a problem I'm really having trouble with. I am using this code to retrieve the parent controller of a UIViewController that is pushed to the UINavigationController stack:
MyAppDelegate *delegate = [[UIApplicationDelegate sharedApplication] delegate];
UINavigationController *nav = delegate.navigationController;
MyParentViewController *parent = (MyParentViewController *)nav.topViewController;
parent.myProperty = @"propertyValue";
However, this seems to only work when you are working with an application with a single navigation controller. My structure is:
->UITabBarController
-->UINavigationController
--->MyYetAnotherParentViewController
-->UINavigationController
--->MyOtherParentViewController
-->UINavigationController
--->MyParentViewController
which means that I have 3 navigation controllers inside the tab bar controller.
I am currently in the third navigation controller and have pushed a view controller above MyParentViewController.
I am trying to pass data from the UIViewController I pushed to MyParentViewController using properties. How will I retrieve the parent of the UIViewController I pushed to the stack if I have this setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否是您正在寻找的,但我认为它是 [nav viewControllers] 数组中最顶层的 ViewController。
Not sure if this is what you are looking for, but i think it is the top-most ViewController in [nav viewControllers] array.
即使您能够使用此方法来实现这一点,但这与 MVC 设计模式并不一致。理想情况下,您的子 VC 应该调用您的模型类的方法(可能是单例)&模型类应该通知你的父 VC。你可以做类似的事情——
Even if you are able to achieve this using this method, this does not align well with the MVC design pattern. Ideally, your child VC should call a method of your model class (which may be a singleton) & the model class should notify your parent VC. You can do something like-