我有带导航控制器的ViewController 1,我想推送其他一些VC。但我在它上面显示的ViewController 2不在导航控制器层次结构中。在ViewController 2中,我有一些方法可以将其最小化,但我无法在ViewController 1中访问导航控制器以将其他VC推送到那里。我尝试从窗口进行代码:
UITabBarController *mTabBarVC = (UITabBarController *) [UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *prevNavigationController = myTabBarVC.selectedViewController.navigationController;
但是我没有成功,我在prevNavigationController中获得了nil。有人能帮助我吗?
答案 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.