如何在UIView中获取父导航控制器 [英] How to get parent navigation controller in UIView
问题描述
我在我的app委托中创建了一个 UITabBarController
。
其中每个标签栏项目包含一个不同的 UINavigationController
,它使用NIB加载自定义 UIViewController
(使用 -pushViewController:
)。
在其中一个导航控制器中,我还加载了一个带有自定义NIB的自定义 UIView
类。
此视图在 UIViewController
内多次加载。
I have created a UITabBarController
in my app delegate.
where each tab bar item holds a different UINavigationController
that loads a custom UIViewController
with a NIB (using -pushViewController:
).
Inside one of the navigation controller, I load a custom UIView
class with a custom NIB also.
This view is loaded multiple times inside the UIViewController
.
自定义 UIView
有一个 UIButton
,如果触摸它,我想推一个新的 UIViewController
在堆栈上。
问题是我丢失了 UINavigationController
,其中包含 UIViewController
。
我知道我应该使用代表,但是还没弄清楚谁应该成为代表。
The custom UIView
has a UIButton
, that on the event of touching it, I want to push a new UIViewController
on the stack.
Problem is that I 'lost' the UINavigationController
that holds the UIViewController
.
I know I should use delegates, but haven't figured out who should which class should be the delegate.
提前致谢!
推荐答案
.navigationController或.tabBarController都不适用于已经创建但未被压入堆栈的UIView或UIViewController
Neither .navigationController or .tabBarController will be available for a UIView or UIViewController that's been created but not pushed onto a stack
在View(或ViewController)类上创建一个属性,该属性是初始化后可选提供的UIViewController,或者您可以向 initWithNibName添加第三个参数:bundle:
Either create a property on your View (or ViewController) class that is a UIViewController that is provided optionally after initialization or you could add a third argument to initWithNibName:bundle:
@interface CustomViewController : UIViewController
{
UIViewController *owner;
}
@property (nonatomic, assign) UIViewController* owner;
@end
然后在所有者ViewController中:
Then in the owner ViewController:
CustomViewController *cvc = [[CustomViewController alloc] initWithNibNamed:nil bundle:nil];
cvc.owner = self;
太糟糕了.parentViewController是只读的,这将是一个合理的地方。
It's too bad .parentViewController is read-only, this would be the sensible place for this.
这篇关于如何在UIView中获取父导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!