2 番目のビュー コントローラーで、というプロパティを作成し、theTextそれNSStringを;にviewDidLoad割り当てます。label.textNSString
- (void)viewDidLoad
{
if(self.theText)
self.label.text = self.theText;
}
次に、最初のビュー コントローラーを使用して、2 番目のビュー コントローラーを設定theTextします。
セグエを使用している場合prepareForSegue:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"Second View Segue"])
{
SecondViewController *theController = segue.destinationViewController;
theController.theText = @"Some text";
}
}
ある種のモーダル プレゼンテーションを使用している場合:
SecondViewController *theController = [[SecondViewController alloc] init];
theController.theText = @"Some text";
[self presentModalViewController:theController animated:YES];
または、ナビゲーションコントローラーを使用している場合:
SecondViewController *theController = [[SecondViewController alloc] init];
theController.theText = @"Some text";
[self.navigationController pushViewController:theController animated:YES];
したがって、最初のView ControllerはNSString2番目にプロパティを設定し、次に2番目はロード中ににUILabel等しい値を設定しNSStringます。2 番目のビュー コントローラーが読み込まれる前に a のテキストを設定することはできないUILabelため、次のようになります。
SecondViewController *theController = [[SecondViewController alloc] init];
theController.label.text = @"Some text";
[self.navigationController pushViewController:theController animated:YES];
ビューが読み込まれるまでラベルのテキストを設定できないため、正しく機能しません。
それが役立つことを願っています。