问题
I am trying to load a storyboard file from a XIB file when a button is clicked. So in the IBAction
method I have called the following line:
- (IBAction)NextView:(id)sender
{
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
[mainStoryBoard instantiateViewControllerWithIdentifier:@"StoryViewController"];
}
But I get this error when I run the application
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x88672d0>) doesn't contain a view controller with identifier 'StoryViewController''
I check out other questions asked by people on stackverflow and found that generally this error is thrown when one forgets to put the identifier name of the View controller in the identity inspector for the view controller that one is trying to load. But I have done that too.
In the storyboard the first viewController which is getting loaded is StoryViewController
and I have set its identifier the same. Is there something else that I may be missing. Please tell me how to correct it.
回答1:
You are setting the class name of the view controller only. I can see that the storyboard ID filed as empty. Set the Storyboard ID to StoryViewController in the Identity section
- (IBAction)NextView:(id)sender
{
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
StoryViewController *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"StoryViewController"];
[self presentViewController:storyViewController animated:YES completion:nil]; // present it
//[self.navigationController pushViewController:storyViewController animated:YES];// or push it
}
来源:https://stackoverflow.com/questions/16713460/how-to-load-a-storyboard-from-a-xib-file