【问题标题】:Unable to change the text of UILabel无法更改 UILabel 的文本
【发布时间】:2012-07-18 21:08:07
【问题描述】:

我有两个视图,变量是从我的 TestViewController 加载的,它是一个 UITableViewController。下面是我认为有用的代码部分。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:@"Section"];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.viewController.detailSec = selected;
    NSLog(@"%@",selected);
}

在我的 ViewController 中,以下代码应该将我的 UILabel 文本更改为我的变量

-(void)setDetailSec:(id)newDetailSec {

    if (_detailSec != newDetailSec) {
        [_detailSec release];
         _detailSec = [newDetailSec retain];
        [self configureView];
    }
    NSLog(@"Step 2 %@",_detailSec);
}


-(void)configureView {
   if (self.detailSec) {
       self.detailLabelChord.text = [_detailSec description];
    NSLog(@"Step 3 %@", _detailSec);
}

如您所见,我添加了 NSLogs 以检查是否调用了 if 函数以及变量是否相应更改并且确实如此!但我的 UILabel 不会变!

2012-07-18 22:03:26.077 testapp[17332:11303] Step 3 CHS 21.3x3.2
2012-07-18 22:03:26.078 testapp[17332:11303] Step 2 CHS 21.3x3.2
2012-07-18 22:03:26.079 testapp[17332:11303] Step 1 CHS 21.3x3.2

有什么想法或建议吗?如果您需要更多信息,我不知道是否可以上传我的项目。

提前谢谢你

【问题讨论】:

  • 你在 View Controller 的实现中 @synthesize detailLabelChord 了吗?
  • 你为什么要在每次选择时添加一个新的 Viewcontroller,是不是因为它被覆盖了所以你看不到正确的 Viewcontroller?
  • 您很可能会丢失对self.detailLabelChord 的引用,因为您一直在重置self.viewController。我猜self.detailLabelChordUILabel 上的ViewController 笔尖?
  • @Jon Friskics 是的,我已经合成它并在我的 .xib 中引用了它
  • @iOS 我应该在哪里添加我的 ViewController?如果您正在谈论“self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];”,这是我发现在 ViewController 中传输变量的唯一方法还有其他方法吗?

标签: ios xcode ipad uilabel uitableview


【解决方案1】:

不要这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:@"Section"];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.viewController.detailSec = selected;
    NSLog(@"%@",selected);
}

这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selected = [[_sectionsArray objectAtIndex:indexPath.row] objectForKey:@"Section"];
    self.viewController.detailSec = selected;
    NSLog(@"%@",selected);
}

每次选择一行时,无需从笔尖初始化新的viewController。您已经拥有viewController。当您创建一个新的viewController 时,您正在这个尚未显示的新控制器上调用setDetailSec。你不是在viewController 上调用它,你可以通过UILabel 看到它。

【讨论】:

  • 通过删除我的 ViewController alloc 变量不会在 ViewController 中调用。我的日志结果是。仅限“2012-07-18 22:49:43.373 testapp[18920:11303] 步骤 1 CHS 21.3x2.3”。
  • 你有没有在你的TestViewController 中初始化过viewController?我会尽早初始化 viewController 并且只做一次,而不是每次选择一行。您没有将viewController 属性链接到正在显示的视图。你只是在创建一个新的viewController,它甚至没有被显示。
  • 您能否更具体地了解初始化?抱歉我的问题,但这不是我的领域,这是我第一次认真对待 xcode。
  • 当你使用 self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 时,你正在创建一个全新的 ViewController 并将对它的引用保存为 self.viewController。这个新的ViewController 没有显示,它只是坐在虚拟土地上并连接到self.viewController。它与您尝试使用UILabel 访问的类型相同,但它是它的全新副本。您需要使self.viewController 指向正在显示的正确ViewController。您是如何创建ViewController 的?
  • @class ViewController;在我的 .h 文件和属性 ViewController *viewController 中。在我的 .m 文件中,我使用了 #import "ViewController.h" 并合成了它。这是正确的方法吗?很抱歉以这种方式发布,但我无法添加代码,我只能在此评论中使用“at”符号一次。
猜你喜欢
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 2019-05-22
  • 1970-01-01
相关资源
最近更新 更多