故事板和自定义初始化 [英] Storyboard and custom init

查看:23
本文介绍了故事板和自定义初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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

做这样的事情可以吗:

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

推荐答案

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

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

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

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

这篇关于故事板和自定义初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆