找出用于UIViewController的NSBundle

时间:2012-07-25 03:09:45

标签: ios5 nsbundle

当使用- (id)initWithNibName:bundle:实例化UIViewControllers时,您可以传入一个包。

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

我有一个容器UIViewController,我想使用创建父的同一个包来实例化它的子UIViewController。除了将捆绑保存为ivar之外,还有其他方法可以实现吗?

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

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

1 个答案:

答案 0 :(得分:0)

UIViewController有一个名为nibBundle的属性,其中包含您要查找的内容:

UIViewController class reference:

  

<强> nibBundle
  返回接收者的nib包的名称(如果存在)。   (只读)

     

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

     

<强>状况
  适用于iOS 2.0及更高版本   另见
  - initWithNibName:bundle:
    @property nibName
  声明
  UIViewController.h

所以,你可以使用:

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