【问题标题】:Going back from a UIViewController to the first ViewController从 UIViewController 返回到第一个 ViewController
【发布时间】:2016-01-13 13:35:39
【问题描述】:

手头的问题如下:我展示了一个 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。

我也尝试了 unwind segue,但是当我按下按钮时它会立即执行此操作,而不是显示 UIAlertController。

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

还有什么我可以尝试的想法吗?

【问题讨论】:

  • DetailViewController,是模态呈现还是嵌入UINavigationController?
  • @bevroy 这是一个推送(显示)

标签: ios swift uiviewcontroller segue


【解决方案1】:

您应该将代码放入 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)
}

【讨论】:

  • 如果我尝试这个,UIAlertController 在我点击“是”或“取消”按钮之前就已经消失了。由于某种原因,它似乎根本没有等待完成。
  • 这确实是正确的答案。我昨天让它与 popViewControllerAnimated 一起工作。感谢您的回答。
【解决方案2】:

您应该使用 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 
            }

        })

【讨论】:

  • 我尝试使用您的代码,但如果我在 didTapDeleteButton == true 下执行dismissViewControllerAnimated(true, completion: nil),则没有任何反应。当我在 else 下执行此操作时,它甚至在我按下“是”按钮之前就关闭了 UIAlertController。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多