让您的 while 循环在后台异步运行:
-(IBAction)function1
{
dispatch_queue_t backgroundQueue = dispatch_queue_create("loopy-loop-background-queue", NULL);
dispatch_async(backgroundQueue, ^(void) {
while ( globalVar==0 )
{
//instruction
_globalCount++;
if( (_globalCount % 10) == 0)
{
[self performSelector:@selector(updateDisplay:)];
}
}
});
}
请记住将任何 UI 更新放回主队列:
-(void)updateDisplay:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
self.outletTimerLabel.text = [NSString stringWithFormat:@"%d", _globalCount];
[self.outletTimerLabel setNeedsDisplay];
});
}