viewController custom init method with storyboard

Nicolas S picture Nicolas S · Mar 18, 2012 · Viewed 28.2k times · Source

im having trouble overriding the initialization method for my CustomViewController thats designed in my Storyboard.

now im doing (in my mainViewController):

self.customViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomVC"];
self.customViewController.myObject = someObject;

and i have in viewDidLoad (CustomViewController)

    [self.label setText:self.myObject.someString];

This works ok.

But, is it the correct way? Should i add a custom init method (or override) to my CustomViewController ? Like initWithObject: ? I dont know how to call my custom init method instead of UIStoryboard instantiateViewControllerWithIdentifier:, and im not getting calls to init nor initWithNibName.

Maybe i should use: - (id)initWithCoder:(NSCoder *)decoder.

Please give me some advice!

Thank you!

Answer

retainCount picture retainCount · Mar 23, 2012

The designated initializer for view controllers in storyboards is -initWithCoder:. Since most view controllers from a storyboard are created via segues, you usually see state set during -prepareForSegue:sender:.

If you're instantiating a view controller directly from the storyboard like in the example you provided, then the pattern you've selected is appropriate.