[英]Getting Parent of A View Controller Pushed on a UINavigationController stack in a UITabBarController with Multiple UINavigationControllers
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:
我正在使用此代码来检索推送到 UINavigationController 堆栈的 UIViewController 的父控制器:
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.这意味着我在标签栏控制器中有 3 个导航控制器。
I am currently in the third navigation controller and have pushed a view controller above MyParentViewController.我目前在第三个导航控制器中,并在 MyParentViewController 上方推送了一个视图控制器。
I am trying to pass data from the UIViewController I pushed to MyParentViewController using properties.我正在尝试使用属性将数据从我推送到 MyParentViewController 的 UIViewController 传递。 How will I retrieve the parent of the UIViewController I pushed to the stack if I have this setup?
如果我有这个设置,我将如何检索我推送到堆栈的 UIViewController 的父级?
不确定这是否是您要查找的内容,但我认为它是 [nav viewControllers] 数组中最顶层的 ViewController。
Even if you are able to achieve this using this method, this does not align well with the MVC design pattern.即使您能够使用这种方法实现这一点,这也与 MVC 设计模式不一致。 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.
理想情况下,您的子 VC 应该调用您的模型类(可能是单例)的方法,并且模型类应该通知您的父 VC。 You can do something like-
你可以做类似的事情——
//In your child VC
[[MyModelClass sharedInstance] buttonClickedInChildVC:@"propertyValue"];
//In your Model class
[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonClickedInChildVC" object:@"propertyValue"];
//In your Parent VC
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(buttonClickedInChildVC:)
name:@"buttonClickedInChildVC"
object:nil];
-(void)buttonClickedInChildVC:(NSNotification *) notification
{
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.