2013-07-19 84 views
-1

所有:的setTitle:forState:在dispatch_after不起作用()

我想 I)禁用與伯爵 二)改變其所有權忙碌的標題的按鈕,和的延遲之後delta秒 iii)啓用按鈕和 iv)將其標題更改爲計數

這一切都應該發生在UIButton操作的代碼中。

我能做的步驟i)至iii),但步驟iv)不工作:

[sender setEnabled:NO]; // the button is now disabled, clicking on it has no effect 
[sender setTitle:@"Busy ... " forState:UIControlStateDisabled]; 


// wait delay seconds before reenabling the count button 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delta * NSEC_PER_SEC), 
       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ 
       [self.countBTN setEnabled:YES]; 
       [self.countBTN setTitle:@"Count" forState:UIControlStateReserved]; 
       }); 

// after delta seconds, the button is re-enabled, but its title is still "Busy ..." 

任何見解,爲什麼這不工作會大大讚賞

回答

4

您需要在主線程上運行UI代碼,而不是後臺線程。

變化:

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 

到:

dispatch_get_main_queue() 

編輯:

您可能還需要改變UIControlStateReservedUIControlStateNormal如果要更改按鈕的主標題。

+0

非常感謝你* Maddy - 那就是訣竅! –