加载从情节提要实例化的 NIB 文件



所以我对这个故事板的概念很陌生。我有一个视图笔尖放入故事板,每个笔尖对应于我拥有的 UIViewController 子类,我尝试使用以下代码加载笔尖文件:

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''

所以问题是,如果我在故事板中有笔尖,是否可以使用 initWithNibName?如果有办法,我该怎么做?

请使用

[self.storyboard instantiateViewControllerWithIdentifier:@"some identifier you set in IB"];

为什么要将 PNRNewsfeedViewController 加载为 TestViewController?TestViewController 是 PNRNewsfeedViewController 的基类吗?确保在 nib 中,文件的所有者自定义类设置为 PNRNewsfeedViewController,然后分配新闻源视图控制器:

PNRNewsfeedViewController *vc = [[PNRNewsfeedViewController alloc] initWithNibName:@"PNRNewsfeedViewController" bundle:nil];
// ...

使用情节提要时,不会直接使用单个笔尖,也不会自己实例化视图控制器。可以使用 UIStoryboard 类从情节提要实例化视图控制器,如文档中所述。

这是我的做法:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

最新更新