objective-c 故事板和自定义初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18331751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:55:36  来源:igfitidea点击:

Storyboard and custom init

objective-cuiviewcontrollerstoryboard

提问by Mark Molina

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.

我最近尝试在 Xcode 中使用 MainStoryboard.storyboard ,到目前为止效果很好,我想知道为什么我以前从未使用过它。在玩一些代码时,我遇到了一个障碍,我不知道如何解决这个问题。

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(使用我在 ViewControllers 类中声明的自定义初始化)时,我会做这样的事情:

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.

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

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?

在使用情节提要的同时,如何仍然使用 myViewController 的自定义初始化?

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

采纳答案by carloabelli

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 initWithCustomDatamethod does is set one instance variable, you should just set it manually (no custom inits or extra methods required):

如果你的initWithCustomData方法所做的只是设置一个实例变量,你应该手动设置它(不需要自定义初始化或额外的方法):

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

回答by Tahir Iqbal

You can instantiate viewcontroller in -init method.

您可以在 -init 方法中实例化视图控制器。

 - (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

并在您的自定义 init 方法中

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

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

   return  self;
 }

回答by Tash Pemhiwa

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

...一个初始化程序;)