Use initWithNibName with a storyboard

2019-05-18 10:42发布

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

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-18 11:17

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-18 11:19
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
TerminalViewController *ctrl = [stryBoard instantiateViewControllerWithIdentifier:@"ControllerView"];
查看更多
不美不萌又怎样
4楼-- · 2019-05-18 11:27

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"];
查看更多
登录 后发表回答