情节提要和自定义init [英] Storyboard and custom init

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

问题描述

我最近尝试在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类中声明的自定义init)时,我会执行以下操作:

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方法都设置了一个实例变量,则应该手动进行设置(无需自定义init或其他方法):

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

这篇关于情节提要和自定义init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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