Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewController custom init method with storyboard

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!

like image 773
Nicolas S Avatar asked Mar 18 '12 06:03

Nicolas S


People also ask

How do I add a ViewController to a storyboard?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.

What is UIViewController in IOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

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.

like image 140
retainCount Avatar answered Sep 28 '22 10:09

retainCount