简体   繁体   中英

How to load a storyboard from a XIB file?

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. 在此处输入图片说明

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

 }

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