gpt4 book ai didi

objective-c - 使用带有 Storyboard的 instantiateViewControllerWithIdentifier 传递属性值

转载 作者:可可西里 更新时间:2023-11-01 06:23:35 25 4
gpt4 key购买 nike

抱歉,我的术语有点偏离, Objective-C 的新手!我正在尝试将一个值从一个 UIViewController 类传递到另一个类。我正在使用 Storyboard。我可以使用以下代码显示第二个 ViewController:

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

这很好用。在我的第二个 ViewController 的 (FormView) 头文件中,我设置了一个属性,如下所示:

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

如何将值从我的第一个 Controller “传递”到第二个 Controller ?据我所知,我必须像这样设置属性:

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

这给了我错误:在“UIViewController”类型的对象上找不到属性“selectedBooking”。

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

谢谢

最佳答案

你需要施放它。

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

那么你的其余代码就可以工作了:D

关于objective-c - 使用带有 Storyboard的 instantiateViewControllerWithIdentifier 传递属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com