簡體   English   中英

將視圖控制器的父級推送到具有多個 UINavigationController 的 UITabBarController 中的 UINavigationController 堆棧上

[英]Getting Parent of A View Controller Pushed on a UINavigationController stack in a UITabBarController with Multiple UINavigationControllers

所以這里有一個問題,我真的遇到了麻煩。 我正在使用此代碼來檢索推送到 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 的父級?

不確定這是否是您要查找的內容,但我認為它是 [nav viewControllers] 數組中最頂層的 ViewController。

即使您能夠使用這種方法實現這一點,這也與 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM