2012-07-25 41 views
0

当使用- (id)initWithNibName:bundle:实例化UIViewControllers时,可以传入一个包。找出用于UIViewController的NSBundle

有没有办法让实例化的视图控制器找出它的原始包是什么?

我有一个容器UIViewController,我想实例化它的孩子UIViewController使用相同的包与父母创建。除了将这个包保存为一个ivar之外,还有另外一种方法来解决这个问题吗?

ContainerViewController *pvc = [[ContainerViewController alloc] 
initWithNibName:nil bundle:[NSBundle mainBundle]]; 

// inside ContainerViewController: 
ChildViewController *cvc = [[ChildViewController alloc] 
initWithNibName:nil bundle:parentBundle]; 

回答

0

UIViewController有一个名为nibBundle属性,它包含了你在找什么:

UIViewController class reference:

nibBundle
接收器返回的笔尖捆绑的,如果它存在的名称。 (只读)

@property(nonatomic, readonly, retain) NSBundle *nibBundle

状况
可用在IOS 2.0和更高。
请参见
- initWithNibName:bundle:
@property nibName
宣布
UIViewController.h

所以,你可以使用:

ChildViewController *cvc = [[ChildViewController alloc] initWithNibName:nil bundle:self.nibBundle]; 
+1

谢谢 - 我莫名其妙地错过了nibBundle财产上UIViewController类。 – 2012-07-25 15:39:34