将视图控制器的父级推送到具有多个 UINavigationController 的 UITabBarController 中的 UINavigationController 堆栈上

发布于 2024-12-05 08:21:19 字数 874 浏览 2 评论 0原文

所以这是一个我真正遇到麻烦的问题。我使用此代码来检索被推送到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2024-12-12 08:21:19

不确定这是否是您正在寻找的,但我认为它是 [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.

人海汹涌 2024-12-12 08:21:19

即使您能够使用此方法来实现这一点,但这与 MVC 设计模式并不一致。理想情况下,您的子 VC 应该调用您的模型类的方法(可能是单例)&模型类应该通知你的父 VC。你可以做类似的事情——

//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
}

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-

//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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文