3

私の用語が少しずれている場合はお詫びします。Objective C は初めてです。ある UIViewController クラスから別のクラスに値を渡そうとしています。ストーリーボードを使用しています。次のコードを使用して、2 番目の ViewController を表示できます。

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

それはうまくいきます。2 番目の ViewController の (FormView) ヘッダー ファイルで、次のようにプロパティを設定します。

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

最初のコントローラーから 2 番目のコントローラーに「値を渡す」にはどうすればよいですか? 私が理解している限り、次のようにプロパティを設定する必要があります。

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

これは私にエラーを与えます: プロパティ 'selectedBooking' がタイプ 'UIViewController' のオブジェクトに見つかりません。

プロパティを設定する正しい方法は何ですか? instantiateViewControllerWithIdentifier ではなく、他に使用すべきものはありますか?

ありがとう

4

2 に答える 2

7

キャストする必要があります。

すなわち

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

その後、残りのコードは機能します:D

于 2012-10-03T13:06:45.837 に答える
2

念のため、クラスをインポートして使用したくない場合FormControllerは、次のようにできます。本当にとても便利です:

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

[vc setvalue:@"test" ForKey:@"selectedBooking"]; //KVC を使用

[self presentModalViewController:vc animated:YES];

注: これは、プロパティでのみ機能します。

于 2013-07-01T10:22:39.150 に答える