UILabel没有更新 [英] UILabel not updating

查看:78
本文介绍了UILabel没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起这个基本问题,但现在这让我有些困扰。

Sorry the basic question, but this bugs me for a while now.

我从UITable创建详细信息视图并尝试动态设置其标签,但它们没有更新:

I create a details view from a UITable and try to dynamically set its labels, but they are not updating:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  myObject *tmpObj = [[myObject objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

  myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil];

  [tmpVC.myLabel setText:tmpObj.myTitle];   // The debugger shows the text: myTitle = "myText"
  NSLog(@"%@", tmpVC.myLabel);              // NSLog SHOWS NULL

  [self.navigationController pushViewController:tmpVC animated:YES];
  [tmpObj release];
}

设置Interface Builder中的连接。文件所有者的连接选项卡显示

the connections in Interface Builder are set. The Connections Tab for File Owner shows

'myLabel' - 'Label (myLabel)'

为什么价值未通过的任何想法?

any ideas why the value is not coming through?

更多观察:


  • 我还连接了IBAction。
    当我
    点击连接按钮时,正确调用此方法。

  • 我得到了一些指向我的
    NSLog语句的指针,无论是$
    不能更好地使用tmpVC.myLabel.text,
    但尝试也返回NULL。

  • myLabel在界面中声明为IBOutlet
    UILabel * myLabel。
    该属性定义为非原子,
    保留。

有光:

在玩了一下之后,我将pushViewController语句移到了标签更新之上。这解决了标签更新。

After playing around with it for a bit more I moved the pushViewController statement above the label updates. That resolved the label updates.

工作代码如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 myObject *tmpObj = [[myObject objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

 myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil];

 [self.navigationController pushViewController:tmpVC animated:YES];

 [tmpVC.myLabel setText:tmpObj.myTitle];   // The debugger shows the text: myTitle = "myText"
 NSLog(@"%@", tmpVC.myLabel);              // NSLog SHOWS NULL

 [tmpObj release];
}

但我不明白为什么我需要先推动我的viewController? ?

But I don't understand why I need to push my viewController first ???

推荐答案

这是因为只有在访问时才会懒惰地创建控制器的视图。推送控制器访问视图。

That's because the controller's view is lazily created only when accessed. Pushing the controller accesses the view.

或者,如果你添加一行来访问view属性,它也会工作:

Alternatively, if you add a line to access the view property, it will work too:

  myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil];
  tmpVC.view;   // Force view creation
  [tmpVC.myLabel setText:tmpObj.myTitle];   // The debugger shows the text: myTitle = "myText"
  NSLog(@"%@", tmpVC.myLabel);              // NSLog will display "myText"
  [self.navigationController pushViewController:tmpVC animated:YES];

这篇关于UILabel没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆