在您的第二视图控制器创建一个名为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