2016-02-04 19 views
0

我試圖讓一個unwind segue取決於用戶在警報對話框中批准它。但是,當我運行代碼時,對話框會很快出現,消失,然後轉入另一個視圖控制器。我試着讓alertDialog成爲類的實例變量,並沒有什麼不同。下面的代碼在UIViewController子類中。我錯過了什麼?presentViewController在shouldPerformSegueWithIdentifier中不是模態

謝謝。

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool { 
    var shouldPerformSeque = true 

    if identifier == "startOver" { 
     let alertDialog = UIAlertController(title: "Warning", 
      message: "This will discard all data entered.", 
      preferredStyle: .Alert) 

     let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 
     alertDialog.addAction(okAction) 

     let cancelActionHandler = { 
      (action: UIAlertAction!) -> Void in 
      shouldPerformSeque = false 
     } 
     let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, 
      handler: cancelActionHandler) 
     alertDialog.addAction(cancelAction) 

     presentViewController(alertDialog, animated: false, completion: nil) 
    } 

    return shouldPerformSeque 
} 
+1

您不能在'shouldPerformSegueWithIndentifier'中執行此操作,因爲該對話框將針對該方法異步呈現;到動作處理程序閉包執行時,方法已經返回。您需要顯示對話框以響應觸發segue的任何內容,然後根據用戶的響應以編程方式執行展開 – Paulw11

+0

@ Paulw11,這應該是一個答案。 –

+0

謝謝。我有這個工作使用提供的解決方案。但是,當我從故事板編輯器中的退出segue斷開按鈕動作時,它刪除了我的seque。我不得不添加一個連接到seque的殘疾和隱藏故事板按鈕來保留它。 – user2129643

回答

0

您不能在shouldPerformSegueWithIndentifier中這樣做,因爲對話框將以該方法異步呈現;到動作處理程序閉包執行時,方法已經返回。

您需要顯示對話框以響應觸發segue的任何內容,然後根據用戶的響應以編程方式執行展開。

相關問題