故事板和自定义初始化

Mar*_*ina 26 objective-c storyboard uiviewcontroller

我最近尝试使用Xcode中的MainStoryboard.storyboard并且到目前为止它已经相当不错了,我想知道为什么我以前从未使用它.在玩一些代码时,我遇到了障碍,我不知道如何解决这个问题.

当我分配并初始化一个新的ViewController(我在ViewControllers类中声明了一个自定义init)时,我会做这样的事情:

ViewController *myViewController = [[ViewController alloc] initWithMyCustomData:myCustomData];
Run Code Online (Sandbox Code Playgroud)

之后我可以做类似的事情:

[self presentViewController:myViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

当我使用故事板时,我了解到切换到独立的ViewController需要一个标识符.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
[self presentViewController:myViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

在使用故事板时,如何仍然使用myViewController的自定义初始化?

只做这样的事情是可以的:

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;
}
Run Code Online (Sandbox Code Playgroud)

car*_*lli 18

我只想创建一个自定义数据加载方法.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
[myViewController loadCustomData:myCustomData];
[self presentViewController:myViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

如果所有initWithCustomData方法都设置了一个实例变量,则应该手动设置它(不需要自定义inits或额外方法):

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifier"];
myViewController.iVarData = myCustomData;
[self presentViewController:myViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.似乎很疯狂Apple没有提供一种使用Storyboard创建正确的自定义`init`方法的方法. (16认同)
  • 我不知道为什么有人会说:“如果你的 initWithCustomData 方法所做的只是设置一个实例变量,你应该手动设置它(不需要自定义初始化或额外的方法)”。没有来自 Apple 的文档说明这一点,因此我建议任何读者将此声明视为仅仅是意见,而不是要遵循的规则。iOS 中的大量初始化器甚至只接受一个参数。 (2认同)

小智 17

您可以在-init方法中实例化viewcontroller.

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

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

   if(self)
   {
    //default initialization

   }
   return  self;
 }
Run Code Online (Sandbox Code Playgroud)

和您的自定义init方法

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

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

   return  self;
 }
Run Code Online (Sandbox Code Playgroud)