在您的第二視圖控制器創建一個名爲theText
屬性,該屬性是一個NSString
然後在viewDidLoad
分配label.text
到NSString
;
- (void)viewDidLoad
{
if(self.theText)
self.label.text = self.theText;
}
現在使用你的第一個視圖控制器在第二個視圖控制器中設置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];
因此,您的第一個視圖控制器將設置NSString
屬性在第二種情況下,第二種設置UILabel
等於NSString
。你不能設置一個UILabel
文本第二視圖控制器被加載之前,所以是這樣的:
SecondViewController *theController = [[SecondViewController alloc] init];
theController.label.text = @"Some text";
[self.navigationController pushViewController:theController animated:YES];
將無法正常工作,因爲直到視圖被加載,你不能設置文本標籤。
希望有所幫助。
你說「第二頁出現」。這是一個重要的細節......這是怎麼發生的?你需要的答案是不同的,取決於是否存在涉及到的代碼或代碼中的某些事情。 – 2012-07-23 21:46:42