2013-02-19 48 views
2

我有以下流程:resignFirstResponder與後續處理的問題(鍵盤解僱iPhone 5的阻塞或崩潰)

Input in Textfield -> (Return key of TextField pressed: resignFirstResponder, IBAction of Button called) -> IBAction of Button: perform an intensive operation 

的問題是,鍵盤與resignFirstResponder解僱前的密集操作開始。作爲建議的其他職位(如https://stackoverflow.com/a/3452767/1685971)我試圖用:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    [self performSelector:@selector(ibActionMethod:) withObject:sender afterDelay:someTime]; 
    return YES; 
} 

這工作正常,在模擬器或iPhone 4,但導致對iPhone 5(我不明白運行崩潰,因爲我沒有使用performSelectorInBackground:withObject:):

void _WebThreadLockFromAnyThread(bool): Obtaining the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread. 
bool _WebTryThreadLock(bool): Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 

我嘗試另一種選擇是等待resignFirstResponder在主線程與

[textField performSelectorOnMainThread:@selector(resignFirstResponder) withObject:nil waitUntilDone:YES]; 

但有了這個鍵盤是STIL我不立即解散(可能是因爲該方法只等待選擇器resignFirstResponder的返回,而不是等待鍵盤動畫的結束。

有沒有解決這個問題的方法?

+0

我不認爲你的意思是通過發件人? – Bushbert 2013-02-19 08:52:48

+0

@Jonathan:對不起,你的意思是?發件人僅傳遞給IBAction - 但這不是問題。 – FrankZp 2013-02-19 09:24:17

回答

2

試試這個

dispatch_async(dispatch_get_main_queue(), ^{ 
    // your button action 
}); 

我也面臨着同樣的錯誤,通過上述伎倆得到解決。

0

試試這個代碼,

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 

     [textField resignFirstResponder]; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     //your code here 

     }); 

     return YES; 

}

+0

添加一個解釋爲什麼這解決了這個問題將有助於使這個更好的答案。 – ChrisF 2013-02-19 09:09:09

+0

調度的代碼不會在主線程中運行,對吧?我認爲這對我來說是一個問題,因爲在IBAction中有一些UI操作正在執行中...... – FrankZp 2013-02-19 09:25:51