简体   繁体   中英

loading a nib file that is instantiated from a storyboard

So I am pretty new to this story board concept. I have a view nibs dropped in to the storyboard and each corresponding to a UIViewController subclass I have, I tried loading the nib file using the following code:

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];

However, it gives me the following error:

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

So the question is, if I have my nib in the story board is it not possible to use the initWithNibName? If there is a way, how do I do this?

请用

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

Why are you loading PNRNewsfeedViewController as TestViewController? Is TestViewController the base class for PNRNewsfeedViewController? Make sure that in the nib, the File's Owner custom class is set to PNRNewsfeedViewController, and then allocate a news feed view controller:

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

When you use a storyboard, you don't use individual nibs directly, and you don't instantiate view controllers yourself. You can use the UIStoryboard class to instantiate a view controller from the storyboard as described in the documentation .

这就是我这样做的方式:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM