Instantiate a View Controller from a Storyboard in IOS

In a lot of the iOS example projects that Apple provides, such as the scrollview paging example, view controllers are Instantiated from an xib file and not from a storyboard which was introduced in iOS 5.
Instantiating a view controller with a story board is quiet easy you just have to specify the name of the storyboard, ie MainStoryBoard_iPhone without the .storyboard and make sure that you have set the identifier of the view controller that you want to instantiate.
Below is a short example:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

MonkeyViewController mkvc = [storyboard instantiateViewControllerWithIdentifier:@"monkeyView"];

you can then add the view to a scrollView using something like:
[self.scrollView addSubview:mkvc.view];

Leave a Reply

Your email address will not be published. Required fields are marked *


*