ViewController custom init method with storyboard

im having problems redefining the initialization method of my CustomViewController created in my storyboard.

now im does (in my mainViewController):

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

and I have viewDidLoad (CustomViewController)

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

This is working fine.

But is that right? Should I add my own initialization method (or override) in my CustomViewController? How to initWithObject :? I don't know how to call my own init method instead of the UIStoryboard instantiateViewControllerWithIdentifier: and im not getting calls to init and initWithNibName .

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

Please give me some tips!

Thank!

+50
uiviewcontroller ios5 uistoryboard
Mar 18 '12 at 6:30
source share
2 answers

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

If you create an instance of the view controller directly from the storyboard, as in the example you provided, the template you select is appropriate.

+58
Mar 23 2018-12-12T00:
source share

As another "workaround" for this problem, you can use delegation. Create a protocol that will be used as the data source for your subclass of the UIViewController from the storyboard. Comply with this data source protocol, implement it, and use it immediately after loading your subclass of UIViewController:

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

I know ... I don’t like it either, but ...;)

0
May 05 '16 at 9:59 a.m.
source share



All Articles