加载一个从故事板实例化的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'' 

所以问题是,如果我在故事板上有我的笔尖,是不是可以使用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类从storyboard中实例化视图控制器, 如文档中所述 。

这是我会这样做的方式:

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