How to load a storyboard from a XIB file?

本秂侑毒 提交于 2019-12-30 05:35:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!