使用stantiateateControllerControllerIdentifier与情节提要板传递属性值 [英] Passing property value using instantiateViewControllerWithIdentifier with storyboards

查看:130
本文介绍了使用stantiateateControllerControllerIdentifier与情节提要板传递属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,对于我的目标C而言,我的术语有点过头了! 我试图将值从一个UIViewController类传递给另一个.我正在使用情节提要.我可以使用以下代码显示第二个ViewController:

Apologies if my terminology is a bit off, new to objective C! I am trying to pass a value from one UIViewController class to another. I am using storyboards. I am able to display the second ViewController using the following code:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"FormView"];
[self presentModalViewController:vc animated:YES];

那很好.在第二个ViewController的(FormView)头文件中,我设置了一个类似这样的属性:

That works fine. In my second ViewController's (FormView) header file I set a property like so:

@interface FormController : UIViewController {
NSString *selectedBooking;
}
@property (nonatomic, retain) NSString *selectedBooking;
@end

如何将值从我的第一个控制器传递到第二个控制器?据我了解,我必须像这样设置属性:

How do I "pass the value" from my first Controller to the second Controller? As far as I understand I have to set the property like so:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"FormView"];
vc.selectedBooking = @"test";
[self presentModalViewController:vc animated:YES];

这给了我一个错误:在'UIViewController'类型的对象上找不到属性'selectedBooking'.

Which gives me the error: Property 'selectedBooking' not found on object of type 'UIViewController'.

设置属性的正确方法是什么?我还应该使用其他东西而不是那个InstantiateViewControllerWithIdentifier吗?

What is the correct way to set the property? Is there something else I should be using rather that instantiateViewControllerWithIdentifier?

谢谢

推荐答案

您需要强制转换.

FormController *fc = (FormController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"FormView"];
fc.selectedBooking = @"test";
[self presentModalViewController:fc animated:YES];

然后其余的代码将起作用:D

then the rest of your code will work :D

这篇关于使用stantiateateControllerControllerIdentifier与情节提要板传递属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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