简体   繁体   English

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

[英]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.

相关问题 当呈现为模态视图控制器时,ARC UINavigationController堆栈未被释放 - ARC UINavigationController stack not getting deallocated when presented as modal view controller uitabbarcontroller的视图控制器未刷新 - The view controller of a uitabbarcontroller not getting refreshed 从UINavigationController堆栈中选择到UITabBarController - Segue out of a UINavigationController stack to UITabBarController UINavigationController,在堆栈上具有相同视图控制器的两个副本 - UINavigationController with two copies of the same view controller on stack iOS:为什么这个视图 Controller 没有被推送到导航控制器的堆栈中? - iOS: Why isn't this View Controller getting pushed onto the Navigation Controller's stack? 在导航堆栈中找不到Pushed View控制器? - Pushed View controller is not found in the navigation stack? 使用UINavigationController在UITabbarController中不显示完整视图 - Not Displaying Complete View in UITabbarController with UINavigationController UITabBarController中的UINavigationController与视图生命周期有关的问题 - UINavigationController in a UITabBarController problems with view lifecycle UITabBarController和UINavigationController内部的自动旋转视图 - Autorotate view inside UITabBarController and UINavigationController 可可触摸:使用带有多个选项卡的UITabBarController作为UINavigationControllers - Cocoa-Touch: Using UITabBarController with multiple tabs as UINavigationControllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM