iPhone UILabel 在首次加载时未更新

发布于 2024-10-25 01:45:05 字数 984 浏览 4 评论 0原文

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

这是代码片段。再次,正如我所说,只有第一次加载时,它才会显示 xib 中标签中指定的内容,即 label,第二次加载时,它将加载字符串 label.text= @ “玻璃、纸张、罐头、纺织品、卡片、鞋子和书籍”;

-(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];
}

I have a UILabel that does not update on first load but does load correctly on every other subsequent load.

Here is the snippet of code. Again, as I said, on the first load only it will display what's specified in the label in the xib, i.e label, and the second time it will load the string 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2024-11-01 01:45:05

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

I believe that at the time you access self.userProfileVC.label at the first time, the label is still nil because the view itself hasn't been loaded yet. The quick fix is that you must load the view first by putting self.userProfileVC.view; statement at the beginning of the showDetails method.

你怎么敢 2024-11-01 01:45:05

大概当您单击 UIButton 时会调用此代码。因此,在第一次 UI 加载时,我不希望调用您的方法(因为没有按下任何按钮)。为什么不尝试在 viewDidLoad 或类似内容中设置默认文本?

例如:

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

Presumably this code is called when you click your UIButton. So at first UI load I wouldn't expect your method to get called (as no button was pressed). Why don't you try setting up the default text in viewDidLoad or similar?

e.g. something along the lines of:

- (void)viewDidLoad {
    self.userProfileVC.label.text= @"Glass, Paper, Cans, Textiles, Card, Shoes and Books";
}
私藏温柔 2024-11-01 01:45:05

我真是个白痴,我改变了代码的顺序......在标签之前先加载视图。卫生部!

代码片段如下:-(

IBAction)showDetails:(id)sender{

//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";

I am such an idiot I have changed the order of my code....load the view first before the labels. DOH!

Heres the snippet:

-(IBAction)showDetails:(id)sender{

//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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文