2011-03-21 73 views
1

我有一个UILabel,它不会在第一次加载时更新,但会在每次其他后续加载时正确加载。iPhone UILabel在第一次加载时不会更新

以下是代码片段。同样,正如我所说,在第一次加载时,它只会显示xib中标签中指定的内容,即label,并且第二次加载字符串label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";

-(IBAction)showDetails:(id)sender{ 
    NSLog(@"Annotation Click"); 
    self.userProfileVC.title=((UIButton*)sender).currentTitle; 
    if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"]) 
     self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 

    [UIView beginAnimations:@"Curl Page Down" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
         forView:self.view cache:YES];  

    //Adjust the Subview by butting up to status bar 
    self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480); 
    //Add userProfileVC as a subview 
    [self.view addSubview:userProfileVC.view]; 
    [UIView commitAnimations]; 
} 

回答

0

我相信那时的你在第一时间访问self.userProfileVC.label,该label仍然nil因为视图本身尚未加载。快速解决方法是,您必须首先在showDetails方法的开头处添加self.userProfileVC.view;语句来加载视图。

+0

谢谢你的伴侣,你给我的线索我只是移动视图加载第一! – 2011-03-21 16:42:00

0

推测当你点击你的UIButton时会调用这段代码。所以在第一次UI加载时,我不会期望你的方法被调用(因为没有按钮被按下)。你为什么不尝试设置viewDidLoad或类似的默认文本?

例如沿着线的东西:

- (void)viewDidLoad { 
    self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 
} 
+0

已加载什么上面到标签,但现在的信息是所有注释一样大?我有52个如果陈述有不同的信息我将如何去解决这个问题:-) – 2011-03-21 15:36:57

+0

“所有的注释” - 不知道你的意思,因为我不明白你在做什么,你从来没有提到过之前的注释。如果你的意思是你有很多'if isEqualToString:@“Asda”'类型的东西,考虑将标签 - >注解映射存储在一个NSMutableDictionary(或普通的NSDictionary)中。然后,您可以通过标签文本查找注释,而不需要大量的“if”。 – occulus 2011-03-21 17:35:18

0

我是这样一个白痴我改变了我的代码的顺序....在标签之前加载视图。 DOH!

继承人的片段:

- (IBAction为)showDetails:(ID)发送{

//Must load the view before the annotations update the label with the correct label text! 
[UIView beginAnimations:@"Curl Page Down" context:nil]; 
[UIView setAnimationDuration:1]; 

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
         forView:self.view cache:YES];  

//Adjust the Subview by butting up to status bar 
self.userProfileVC.view.frame = CGRectMake(0, 0, 320, 480); 
//Add userProfileVC as a subview 
[self.view addSubview:userProfileVC.view]; 
[UIView commitAnimations]; 

NSLog(@"Annotation Click"); 
self.userProfileVC.title=((UIButton*)sender).currentTitle; 
if([((UIButton*)sender).currentTitle isEqualToString:@"Asda"]) 
    self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books"; 
+0

你可能打算接受提亚的答案,而不是发布自己的答案。如果您想澄清如何最终解决问题,可以编辑您的问题,并在最后加入“解决方案部分”。 – Kalle 2011-03-21 17:39:33

相关问题