另一种方式来做到这一点是块:
大优点:你不需要创建一个额外的方法:)
- (IBAction)btnQuickCheckClick:(id)sender {
//UI changes must be done in the main thread
self.btnQuickCheck.enabled = NO;
//do your thing in a background thread
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT ,0);
dispatch_async(queue, ^(){
@try {
//do your thing here
[self pollRouter];
} @catch (NSException * e) {
//handle the exception, if needed
} @finally {
//change to the main thread again and re-enable the UI
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^(){
self.btnQuickCheck.enabled = YES;
});
}
});
}
这将运行在后台线程pollRouter
。所以如果你没有修改用户界面或其他非线程安全的东西在那里,你想使用这种方法:)否则去@亚历克斯的方法
你自己回答了这个问题。您正在同步进行轮询,所以UI从来没有机会更新以禁用按钮。 – Flyingdiver