Storyboard and custom init

前端 未结 3 1123
醉话见心
醉话见心 2020-12-15 17:51

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

相关标签:
3条回答
  • 2020-12-15 17:59

    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];
    
    0 讨论(0)
  • 2020-12-15 18:02

    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;
     }
    
    0 讨论(0)
  • 2020-12-15 18:07

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

    0 讨论(0)
提交回复
热议问题