NSNotification被调用,但标签不会更新

时间:2012-06-21 16:35:20

标签: iphone ios xcode ios5

我使用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];
    }
}

2 个答案:

答案 0 :(得分:1)

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

请尝试下面的代码,也许它可以帮助您:

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

- (void) theProcess:(id)sender
{

    ....

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

}

答案 1 :(得分:0)

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

[chipcount setNeedsDisplay];

只是一个想法。