2016-01-13 60 views
2

手頭的問題如下:我展示了一個UIAlertController。如果用戶按下刪除鍵,則視圖應該返回到class TableViewQuery: PFQueryTableViewController類的第一頁。從UIViewController回到第一個ViewController

import UIKit 

class DetailViewController: UIViewController { 

    var currentObject : PFObject? 


      @IBAction func pressDelete(sender: AnyObject) { 

       let deleteUser = UIAlertController(title: "Are you sure?", message: "Are you sure you want to delete the current user?", preferredStyle: UIAlertControllerStyle.Alert) 
       deleteUser.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction) -> Void in 

        // Unwrap current object 
        if let object = self.currentObject { 
         object["firstName"] = self.firstName.text 
         object["lastName"] = self.lastName.text    
         object.deleteEventually() 
        } 

      })) 
      deleteUser.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction) -> Void in 
       //Do nothing 
      })) 


      presentViewController(deleteUser, animated: true, completion: nil)   
    } 

我已經試過self.dismissViewControllerAnimated(true, completion: nil),但只是關閉UIAlertController。

我也嘗試了unwind segue,但是當它按下按鈕時,它立即做到了,而不是顯示UIAlertController。

我也試過segueForUnwindingToViewController(<toViewController: UIViewController:UIViewController>, fromViewController: <#T##UIViewController#>, identifier: <#T##String?#>)但是我的toViewController不是UIViewController,而是PFQueryTableViewController。

我還有什麼可以嘗試的任何想法?

+0

'DetailViewController',它是模態呈現還是嵌入在'UINavigationController'中? – bevoy

+0

@bevroy它是一個Push(顯示) – SoundShock

回答

1

當用戶單擊YES時,您應該將代碼放入UIAlertAction的處理程序中以進行響應。我想你正在使用主細節視圖控制器。如果是這樣,你需要找到正確的導航控制器彈出。否則,如果您正在使用導航控制器,只需按照我評論的代碼彈出即可。

@IBAction func pressDelete(sender: AnyObject) { 
    let deleteUser = UIAlertController(title: "Are you sure?", message: "Are you sure you want to delete the current user?", preferredStyle: UIAlertControllerStyle.Alert) 
    deleteUser.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction) -> Void in 
     // If you are using master detail view controllers 
     if let navController = self.splitViewController?.viewControllers[0] as? UINavigationController { 
      navController.popViewControllerAnimated(true) 
     } 
     // If you using navigation controller 
     // self.navigationController?.popViewControllerAnimated(true) 
    })) 
    deleteUser.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction) -> Void in 
     //Do nothing 
    })) 


    presentViewController(deleteUser, animated: true, completion: nil) 
} 
+0

如果我嘗試這個,UIAlertController已經消失,我甚至可以點擊'是'或'取消'按鈕。由於某種原因,它似乎並沒有等待完成。 – SoundShock

+0

@SoundShock我已經更新了我的答案。 –

+0

這確實是正確的答案。我在昨天使用了popViewControllerAnimated。感謝你的回答。 – SoundShock

1

您應該使用presentViewController的完成處理程序。您可能需要跟蹤用戶使用警報視圖控制器執行的任何操作。您必須使用警報視圖控制器操作來跟蹤用戶的選擇。然後在完成處理程序中檢查用戶的選擇,然後相應地進行導航。

var didTapDeleteButton = false 

     @IBAction func pressDelete(sender: AnyObject) { 

      let deleteUser = UIAlertController(title: "Are you sure?", message: "Are you sure you want to delete the current user?", preferredStyle: UIAlertControllerStyle.Alert) 
      deleteUser.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction) -> Void in 

       // Unwrap current object 
       if let object = self.currentObject { 
        object["firstName"] = self.firstName.text 
        object["lastName"] = self.lastName.text    
        object.deleteEventually() 

        didTapDeleteButton = true 
       } 

     })) 
     deleteUser.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction) -> Void in 
      didTapDeleteButton = false 
     })) 

     presentViewController(viewController, animated: true, completion: { 

      // Make navigation choice here 
      if didTapDeleteButton == true { 
       // Navigation option 1 
      } else { 
       // Navigation option 2 
      } 

     }) 
+0

我試圖使用你的代碼,但是如果我在didTapDeleteButton == true下執行'dismissViewControllerAnimated(true,completion:nil)',沒有任何反應。當我在else下執行時,即使在我按下'Yes'按鈕之前,它也會關閉UIAlertController。 – SoundShock

相關問題