手头的问题如下:我展示了一个UIAlertController。如果用户按下删除,则视图应返回到第一页,即class TableViewQuery: PFQueryTableViewController
类。
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。
我也尝试了解开segue,但是当我按下按钮时它会立即执行,而不是显示UIAlertController。
我也尝试了segueForUnwindingToViewController(<toViewController: UIViewController:UIViewController>, fromViewController: <#T##UIViewController#>, identifier: <#T##String?#>)
但我的toViewController不是UIViewController,而是PFQueryTableViewController。
关于我还能尝试什么的任何想法?
答案 0 :(得分: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
}
})
答案 1 :(得分: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)
}