使用instantiateViewControllerWithIdentifier与故事板传递属性值

道歉,如果我的术语是有点closures,新目标C! 我想从一个UIViewController类传递一个值到另一个。 我正在使用故事板。 我能够使用下面的代码显示第二个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 

我如何从我的第一个控制器“传递价值”到第二个控制器? 据我所知,我必须设置属性如下所示:

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

这给了我错误:在'UIViewController'types的对象上找不到属性'selectedBooking'。

什么是设置属性的正确方法? 还有什么我应该使用,而不是那个instantiateViewControllerWithIdentifier?

谢谢

你需要投它。

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

那么你的代码的其余部分将工作:D

以防万一,如果你不想导入类FormController并与它一起,你可以做如下。 真的很方便:

 UIStoryboard *mainStoryboard = .. UIViewController *vc = ... 

[vc setvalue:@“test”ForKey:@“selectedBooking”]; //使用KVC

 [self presentModalViewController:vc animated:YES]; 

注:这将只适用于属性。