从故事板实例化的nib文件如何加载?

3

我对故事板这个概念还比较陌生。我在故事板中添加了一些视图nib文件,每个文件都对应一个我创建的UIViewController子类。我尝试使用以下代码来加载nib文件:

TestViewController *vc = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
    [self.view setBackgroundColor:[UIColor blueColor]];
    [self.view setFrame:CGRectMake(0, self.profilePicture_.frameHeight + self.profilePicture_.frameY + 10, self.scrollView_.frameWidth, 100)];
    [self.view addSubview:vc.view];

然而,它给了我以下错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/aditya15417/Library/Application Support/iPhone Simulator/5.1/Applications/64E6CEC9-E6DC-4AF5-BF16-11BFB6415BDC/Pulse.app> (loaded)' with name 'TestViewController''

所以问题是,如果我在故事板中有我的 nib 文件,那么是否不可能使用 initWithNibName 方法?如果有方法,应该如何操作?

4个回答

7
请使用。
[self.storyboard instantiateViewControllerWithIdentifier:@"some identifier you set in IB"];

1
可以通过编程设置实例的类吗? - MUH Mobile Inc.

0

当您使用故事板时,不直接使用单独的nib文件,并且不自己实例化视图控制器。您可以使用UIStoryboard类从故事板中实例化视图控制器如文档所述


0
为什么要将PNRNewsfeedViewController加载为TestViewController? TestViewController是PNRNewsfeedViewController的基类吗?请确保在nib中,文件所有者自定义类设置为PNRNewsfeedViewController,然后分配一个新闻源视图控制器:
PNRNewsfeedViewController *vc = [[PNRNewsfeedViewController alloc] initWithNibName:@"PNRNewsfeedViewController" bundle:nil];
// ...

抱歉,是的,那只是一个打字错误。实际上我使用了TestViewController。 - adit

0
这是我会做的方式:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接