目标C:如何从我当前的ViewController后面的ViewController获取导航控制器

时间:2016-07-11 18:27:51

标签: ios objective-c uiviewcontroller uinavigationcontroller hierarchy

我有带导航控制器的ViewController 1,我想推送其他一些VC。但我在它上面显示的ViewController 2不在导航控制器层次结构中。在ViewController 2中,我有一些方法可以将其最小化,但我无法在ViewController 1中访问导航控制器以将其他VC推送到那里。我尝试从窗口进行代码:

UITabBarController *mTabBarVC = (UITabBarController *) [UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *prevNavigationController = myTabBarVC.selectedViewController.navigationController;

但是我没有成功,我在prevNavigationController中获得了nil。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

Create in ViewController 2.h:

@property UIViewController *parent;

In ViewController 1 pass self:

viewController2.parent = self;

In ViewController 2 you can now get UINavigationController:

UINavigationController *navVC = self.parent.navigationController;

It's the best way, because you can change structure of your view controllers in future and will get a bug.

If ViewController 2 present modally on ViewController 1, you can get VC1 from VC2 in self.presentingViewController property.