2016-11-01 61 views
2

我以前遇到過類似的問題,UIAlertController,因爲在UIAlertController被解僱後,UI線程總會有滯後。
我現在的問題是,如果用戶點擊「Okay」UIAlertAction,我想執行一個segue,如果按下「Cancel」UIAlertAction,則不會發生任何事情。
這是我的代碼:UIAlertController解除後執行segue

// create the uialertcontroller to display 
let alert = UIAlertController(title: "Do you need help?", 
           message: "Would you like to look for a consultant?", 
           preferredStyle: .alert) 
// add buttons 
let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    self.performSegue(withIdentifier: "segue", sender: nil) 
}) 
let no = UIAlertAction(title: "No, I'm okay.", style: .cancel, handler: nil) 
alert.addAction(okay) 
alert.addAction(no) 
self.present(alert, animated: true, completion: nil) 

什麼正在發生的事情,當我點擊「好」的SEGUE正在執行,但我只能看到過渡的最後時刻(即動畫開始,而UIAlertController是爲被解僱)。
一旦UIAlertController被解僱,我該如何讓這個segue開始?

注 - 我不喜歡用像hacky方法解決這個問題,就像在一個固定的延遲後執行segue,如果還有其他方法的話。

謝謝!

+0

@馬特所以不是一個真正的辦法做到這一點,除非我'n'秒後動畫?此外 - 沒有我說的禁止 – uti0mnia

回答

3

的問題是在此代碼:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    self.performSegue(withIdentifier: "segue", sender: nil) 
}) 

handler:不是完成處理程序。它在之前運行警報被(自動)解除。因此,當警報仍然存在時,您正在啓動繼續。

如果你不喜歡使用delay(雖然我認爲沒有錯的方法),我會嘗試是這樣的:

let okay = UIAlertAction(title: "Yes, please.", style: .default, handler: {_ in 
    CATransaction.setCompletionBlock({ 
     self.performSegue(withIdentifier: "segue", sender: nil) 
    }) 
}) 
+0

我想 - 有沒有什麼辦法可以在控制器被解僱後執行此操作? – uti0mnia

+0

我怎麼告訴你不要?如果你指的是我的筆記,那就意味着我當然更喜歡一種沒有靜態調度的方法:) – uti0mnia

+0

你的答案不包含派遣後,所以我不明白爲什麼想學習一個好的解決方案是不公平的? – uti0mnia