iPhone UILabel首次加载时无法更新

时间:2011-03-21 15:12:52

标签: iphone uilabel

我有一个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];
}

3 个答案:

答案 0 :(得分:0)

当您单击UIButton时,可能会调用此代码。所以在第一次UI加载时我不希望你的方法被调用(因为没有按下按钮)。为什么不尝试设置viewDidLoad或类似的默认文字?

e.g。类似的东西:

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

答案 1 :(得分:0)

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

答案 2 :(得分: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";