Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard and custom init

I recently tried working with the MainStoryboard.storyboard within Xcode and so far It's going pretty good and I'm wondering why I've never used it before. While playing with some code I bumped into an obstacle and I don't know how to resolve this.

When I alloc and init a new ViewController (with a custom init I declared in the ViewControllers class) I would do something like this:

ViewController *myViewController = [[ViewController alloc] initWithMyCustomData:myCustomData]; 

Then after that I could do something like:

[self presentViewController:myViewController animated:YES completion:nil]; 

When I'm working with a storyboard I'm learnt that switching to a standalone ViewController requires an Identifier.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; [self presentViewController:myViewController animated:YES completion:nil]; 

How can I still use my custom initialization for myViewController while making use of a storyboard?

Is it ok to just do something like this:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; myViewController.customData = myCustomData; [self presentViewController:myViewController animated:YES completion:nil];     //MyViewController.m - (id) initWithMyCustomData:(NSString *) data { if (self = [super init]) {     iVarData = data; } return self; } 
like image 905
Mark Molina Avatar asked Aug 20 '13 09:08

Mark Molina


People also ask

How to initialize viewcontroller in swift?

Defining a Custom Initializer. Open ImageViewController. swift and add an initializer with name init(coder:image:) . The initializer accepts an NSCoder instance as its first argument and an Image object as its second argument.

How do I instantiate a view controller from storyboard?

In the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.

What is UIStoryboard?

A UIStoryboard object manages archived versions of your app's view controllers. At design time, you configure the content of your view controllers visually, and Xcode saves the data needed to recreate that interface in a storyboard file in your app's bundle.


1 Answers

I would just create a method which does the custom data loading.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; [myViewController loadCustomData:myCustomData]; [self presentViewController:myViewController animated:YES completion:nil]; 

If all your initWithCustomData method does is set one instance variable, you should just set it manually (no custom inits or extra methods required):

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"]; myViewController.iVarData = myCustomData; [self presentViewController:myViewController animated:YES completion:nil]; 
like image 63
carloabelli Avatar answered Sep 21 '22 02:09

carloabelli