2012-10-03 33 views
3

如果我的术语稍微偏离一点,对目标C新增了道歉! 我想从一个UIViewController类传递一个值到另一个。我正在使用故事板。我能够使用下面的代码显示第二个ViewController:使用instantiateViewControllerWithIdentifier和故事板传递属性值

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

这工作正常。在我的第二个视图控制器的(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]; 

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

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

谢谢

回答

7

您需要施放它。

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

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

+1

谢谢,作品完美,赞赏 –

+1

很高兴我能帮到你。 – Fogmeister

2

以防万一,如果你不希望导入类FormController,并用它去,你可以根据以下步骤做。真的非常方便:

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

[vc setvalue:@"test" ForKey:@"selectedBooking"]; //using KVC

[self presentModalViewController:vc animated:YES]; 

注意:这只会处理其属性。