0

我有以下用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];
    }
}
4

2 回答 2

1

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

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

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

- (void) theProcess:(id)sender
{

    ....

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

}
于 2012-09-19T07:29:31.517 回答
0

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

[chipcount setNeedsDisplay];

只是一个想法。

于 2012-06-21T20:24:07.830 回答