Loading iOS ViewController in Code from a Storyboard File

In one project, I needed to present some one-off view controllers so I thought it would be best to put them in their own StoryBoard file and load them in code.

Screen Shot 2014-01-08 at 12.33.11 PMI wrote a method that takes the view controller name (set in the Identity Inspector – see image). In this case it is “browserVC” (I have a separate one for browserVC_iPhone in the same Storyboard file).

It also takes the story board (file) name, in this case the storyboard is named Browser.storyboard. So the call to the method is:

[self loadAndPresentVCNamed:@”browserVC” fromStoryboardNamed:@”Browser”];

Those are passed into the code to create the instance (and present it)…

1. The code first gets the story board file into a UIStoryboard pointer.

2. Then, based on the UI idiom, it modifies the vcName if necessary.

3-4. Then it gets the view controller instance and presents it. It returns the controller which you may or may not use.

In this case, the returned controller has a method to load a URL. So for my uses, the method above presents the view controller (which happens to be a ‘browser’) and the calling method can specify what URL to load.