Storyboard and custom init

2019-01-23 11:37发布

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;
}

3条回答
2楼-- · 2019-01-23 12:05

My version:

- (instancetype)initWithData (NSArray *)someData
 {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

   self = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

   if(self)
   {
    //default initialization

   }
   return  self;
 }

...one initializer ;)

查看更多
smile是对你的礼貌
3楼-- · 2019-01-23 12:06

You can instantiate viewcontroller in -init method.

 - (instancetype)init
 {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

   self = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

   if(self)
   {
    //default initialization

   }
   return  self;
 }

and the in your custom init method

 - (instancetype)initWithImages:(NSArray *)images
 {
   self = [self init];

   if(self)
   {
     self.images = images;
   }

   return  self;
 }
查看更多
Rolldiameter
4楼-- · 2019-01-23 12:28

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];
查看更多
登录 后发表回答