viewController custom init method with storyboard

Ios5UiviewcontrollerUistoryboard

Ios5 Problem Overview


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!

Ios5 Solutions


Solution 1 - Ios5

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.

Solution 2 - Ios5

As another "workaround" for this problem, you could use delegation. Create a protocol that would act as data source for your UIViewController subclass from storyboard. Conform to this data source protocol, implement it and use it right after loading your UIViewController subclass:

required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        
        //invoke your data source here
    }

I know... I also don't like this but... ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicolas SView Question on Stackoverflow
Solution 1 - Ios5retainCountView Answer on Stackoverflow
Solution 2 - Ios5DespotovicView Answer on Stackoverflow