【问题标题】:NSNotification Called, but label will not updateNSNotification 调用,但标签不会更新
【发布时间】:2012-06-21 16:35:19
【问题描述】:

我用NSNotification 中心调用了以下代码,我知道它被调用是因为数组出现在NSLog 中,但我的标签chipCount 没有用新值更新。从数组中提取字符串时,我是否应用了错误的方法?

-(NSString *) dataFilePath {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@"Chips.plist"];
}

-(void)readPlist {
    [self dataFilePath];
    NSString *filePath = [self dataFilePath];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        chipArray = [[NSArray alloc] initWithContentsOfFile:filePath];
        NSLog(@"%@\n", chipArray);
        NSLog(@"%@\n", filePath);

        NSString *chipcountString = [chipArray objectAtIndex:0];
        chipsFloat = [chipcountString intValue];
        chipCount.text = [NSString stringWithFormat:@"%i", chipsFloat];
        //[arrayForPlist release];
    }
}

【问题讨论】:

  • 是的,它在我的标题中是 IBOutlet UILabel *chipCount;
  • 如果您在标签中没有看到任何内容,我建议您尝试将其设置为常量字符串值(如果您还没有的话)。这至少可以排除您的标签未正确连接。
  • 确保你在主线程中!用户界面只能在那里更改。请参阅此link 了解更多信息。
  • 在这个方法的最后尝试NSLog(@"loaded: %@, converted to: %i", chipcountString, chipsFloat),看看是否是你所期望的。
  • 它连接正确,它是一个子视图,它最初从父视图获取芯片计数,但随后子视图打开另一个视图,该视图编辑值并尝试重置视图中的值使用新值

标签: iphone ios xcode ios5


【解决方案1】:

我认为这是一个多线程问题。更新 UI 必须在主线程中。也许readPlist
在另一个线程中执行。

试试下面的代码,也许它可以帮助你:

[self performSelectorOnMainThread:@selector(theProcess:) withObject:nil waitUntilDone:YES]; 

- (void) theProcess:(id)sender
{

    ....

    chipCount.text = [NSString stringWithFormat:@"%i", chipsFloat];

}

【讨论】:

    【解决方案2】:

    假设 chipcount 是 UILabel.. 是否需要告诉它刷新标签?

    [chipcount setNeedsDisplay];
    

    只是一个想法。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多