简体   繁体   中英

Issues Using .xib With Storyboard

Hey I'm trying to open a view controller that is designed using a xib interface file. I am using the following lines of code to generate the controller from within a view controller created as a storyboard component.

YourViewController *viewController [[YourViewControlleralloc] initWithNibName:@"ViewControllerName" bundle:nil];

[self presentViewController:viewController animated:YES completion:nil]; 

I have never set up a view controller with a .xib before nor have I ever linked to one via storyboard so anything could be wrong. Here is the error I am getting when I try to present the view.

2013-05-17 13:06:45.120 Coffee[8991:907] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "PeekPagedScrollViewController" nib but the view outlet was not set.'

Set the File's Owner in the .xib to the class of your view controller (perhaps it is called PeekPagedScrollViewController).

Then connect its view outlet to the main view in the .xib file.

It was connected properly I had just forgotten to declare an initWithNibName method and therefore it crashed when I called it from storyboard. Silly mistake but thanks for all the great feedback @matt

Set the Storyboard view controller custom class to your class name (the one your xib is also referencing), then in that same class, simply override initWithCoder with:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [[super initWithCoder:aDecoder] initWithNibName:@"YourCustomViewControllerXibsName" bundle:nil];
    if( self ) {
    }
    return self;
}

This will enable you to design layout and segue transitions in Storyboard, but design your layout in a standalone nib file.

Storyboard will handle instantiation, but when initWithCoder: is called, initWithNibName: will be the method that instantiates self via the xib.

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