2016-01-13 65 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

相关问题