问题
Below is an example of using initWithNibName with separate xib views:
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:@"ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
viewCtrl = ctrl;
However i need to implement it with a storyboard UI layout. For 'initWithNibName' how can i point to a View in my storyboard:
i.e. :
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:@"STORYBOARD.ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
Any help is much appreciated. Thanks, Dave
回答1:
You can give the controller an Identifier
in the storyboard and then use this...
[self.storyBoard instantiateViewControllerWithIdentifier:@"TheIdentifier"];
Just to add to this...
When creating a UIViewController
subclass from a xib file. If you have a class called MyViewController
and a xib called MyViewController.xib
then all you need to do is...
MyViewController *controller = [[MyViewController alloc] init];
The system will then look for a xib file called MyViewController.xib
and use that to init the object and only fallback to doing it in code if the xib file doesn't exist.
回答2:
get storyboard instance with name of storyboard and set identifier to all scene and get the instance
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"myViewCont"];
回答3:
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
TerminalViewController *ctrl = [stryBoard instantiateViewControllerWithIdentifier:@"ControllerView"];
来源:https://stackoverflow.com/questions/16913709/use-initwithnibname-with-a-storyboard