我有一個while語句在後臺運行。iOS:performSelectorOnBackground/MainThread:不更新標籤
- (IBAction)startButton
{
[self performSelectorInBackground:@selector(Counter) withObject:nil];
.....
}
- (void) Counter
{
while (round) {
if (condition)
{
NSString *str;
str = [NSString stringWithFormat:@"%d",counter];
[self performSelectorOnMainThread:@selector(updateLabel:) withObject:str waitUntilDone:NO];
}
}
}
- (void)updateLabel: (NSString*) str
{
[self.label setText:str];
NSLog(@"I am being updated %@",str);
}
NSlog獲取正確的更新值,但標籤永遠不會更新。
我在做什麼錯?
更新:
標籤連接,並完成了while語句之後,它就會被更新過的。
另外我已初始化標籤。
- (void)viewDidLoad
{ [super viewDidLoad];
label.text = @"0";
}
我懷疑self.label是零,因爲你的實際標籤是在另一個實例中,或者它根本就沒有連接。 –
你可以展示你如何運行while循環「在後臺」?我懷疑你是在主線程上運行它。 – omz
檢查我的更新:我使用[self performSelectorInBackground:@selector(Counter)withObject:nil]; – alandalusi