如何获得UIView中的父导航控制器

我在我的应用程序委托中创build了一个UITabBarController 。 其中每个标签栏项目都包含一个不同的UINavigationController ,它用一个NIB(使用-pushViewController:加载一个自定义的UIViewController 。 在其中一个导航控制器中,我也加载了一个自定义的UIView类。 这个视图在UIViewController被多次加载。

自定义UIView有一个UIButton ,在触摸它的事件时,我想在堆栈上推新的UIViewController 。 问题是我'失去了' UINavigationController持有UIViewController 。 我知道我应该使用代表,但还没有弄清楚谁应该是代表哪一类。

提前致谢!

没有.navigationController或.tabBarController将可用的UIView或UIViewController已被创build,但没有推到堆栈

可以在View(或ViewController)类上创build一个属性,该类是在初始化之后可select提供的UIViewController,或者可以向initWithNibName:bundle:添加第三个参数initWithNibName:bundle:

 @interface CustomViewController : UIViewController { UIViewController *owner; } @property (nonatomic, assign) UIViewController* owner; @end 

然后在所有者ViewController中:

 CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil]; cvc.owner = self; 

这太糟糕了.parentViewController是只读的,这将是明智的地方。

你可以使用UIApplication类来获得这个。 使用这个类你可以先find哪个viewController 。 这是您的问题的解决scheme链接 。