iphone - UILabel 不更新

标签 iphone objective-c

抱歉这个基本问题,但这让我困扰了一段时间。

我从 UITable 创建了一个细节 View 并尝试动态设置它的标签,但它们没有更新:

- (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 中的连接已设置。文件所有者的连接选项卡显示

'myLabel' - 'Label (myLabel)'

有什么想法没有体现值(value)吗?

更多观察:

  • 我还连接了一个 IBAction。 当我 单击连接按钮。
  • 我得到了一些关于我的指示 NSLog-statement,是否应该 最好不要使用 tmpVC.myLabel.text, 但尝试也返回 NULL。
  • myLabel 声明为 IBOutlet 界面中的UILabel *myLabel。 该属性被定义为非原子的, 保留。

有光:

在进一步尝试之后,我将 pushViewController 语句移至标签更新上方。这解决了标签更新问题。

工作代码如下所示:

- (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 ???

最佳答案

那是因为 Controller 的 View 只有在被访问时才会延迟创建。插入 Controller 访问 View 。

或者,如果您添加一行来访问 View 属性,它也可以工作:

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

关于iphone - UILabel 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2720662/

相关文章:

iphone - 为什么不直接使用 self.view 在 iPhone SDK 中切换 View ?

iPhone 应用程序更新,同时仍在后台

iphone - 控制 UIScrollView 越界?

iphone - iPad 应用程序的按安装付费机制?

objective-c - 从另一个 UIViewController 的 .xib 文件在 ViewController.h 中创建 IBOutlet

iphone - 使用 PhoneGap 并显示 iAd

iphone - 给定一个 CGPath,如何让它弯曲?

iphone - 在 IOS 5 中检测设备是否处于静音模式?

iphone - 滑动手势已添加到 UILabel,但它不起作用

objective-c - 是否有KeyUp : method for flagsChanged:?