Swift 4 UIAlertController 如何将不同的操作合并到 1 个完成处理程序中?

Swift 4 UIAlertController how can I combine different actions into 1 completion handler?

在下面的代码中,我正在检查 UserDefaults。我想根据价值做出反应。第一个问题是如何将这两种情况组合成一个 completionHandler?第二个问题是,在完成处理程序中,我可以找到操作的按钮索引,而不是我的开关依赖于标题吗?提前致谢。

 func checkUser() {

    let registered = UserDefaults.standard.bool(forKey: "registered")

    switch registered {

        case true:
            let firstName = UserDefaults.standard.string(forKey: "firstName") ?? ""
            let lastName = UserDefaults.standard.string(forKey: "lastName") ?? ""
            var fullName: String!
            fullName = firstName
            fullName.append(" \(lastName)")

            let optionMenu = UIAlertController(title: "Please confirm", message: "Are you \n \(String(describing: fullName))", preferredStyle: .alert)

            optionMenu.popoverPresentationController?.sourceView = self.view
            optionMenu.popoverPresentationController?.sourceRect = self.view.bounds
            let yesAction = UIAlertAction(title: "Yes", style:.destructive, handler: confirmHandler)
            let noAction = UIAlertAction(title: "no", style: .destructive, handler: confirmHandler)
            optionMenu.addAction(yesAction)
            optionMenu.addAction(noAction)
            let popover = optionMenu.popoverPresentationController
            popover?.delegate = self
            popover?.sourceView = view
            popover?.sourceRect = CGRect(x: self.view.bounds.midX - 100, y: self.view.bounds.midY + 100, width: 200, height: 200)
            DispatchQueue.main.async {
                self.present(optionMenu, animated: true, completion: {})
            }

        case false:
            let optionMenu = UIAlertController(title: "Please confirm", message: "Do you want to set up this iPad?", preferredStyle: .alert)

            optionMenu.popoverPresentationController?.sourceView = self.view
            optionMenu.popoverPresentationController?.sourceRect = self.view.bounds
            let yesAction = UIAlertAction(title: "Yes", style: .default, handler: setUpHandler)
            let noAction = UIAlertAction(title: "no", style: .default, handler: setUpHandler)
            optionMenu.addAction(yesAction)
            optionMenu.addAction(noAction)
            let popover = optionMenu.popoverPresentationController
           // popover?.delegate = self
            popover?.sourceView = view
            popover?.sourceRect = CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY, width: 0, height: 0)
            DispatchQueue.main.async {
                self.present(optionMenu, animated: true, completion: {})
            }
        }
}

func setUpHandler (alert: UIAlertAction) {

    print ("received: \(String(describing: alert.title))")
    switch alert.title {
        case "Yes":
            print("show set up")
        case "No":
            print("show set up")
        default:
            print("show set up")
    }
}
func checkUser() {

    let registered = UserDefaults.standard.bool(forKey: "registered")

    let title = "Please Confirm"
    var message:String?
    var actions:[UIAlertAction]! = []
    var rect:CGRect!

    switch registered {

        case true:
            let firstName = UserDefaults.standard.string(forKey: "firstName") ?? ""
            let lastName = UserDefaults.standard.string(forKey: "lastName") ?? ""
            var fullName: String!
            fullName = firstName
            fullName.append(" \(lastName)")


            let yesAction = UIAlertAction(title: "Yes", style:.destructive, handler: confirmHandler)
            let noAction = UIAlertAction(title: "no", style: .destructive, handler: confirmHandler)
            actions.append(yesAction)
            actions.append(noAction)
            rect = CGRect(x: self.view.bounds.midX - 100, y: self.view.bounds.midY + 100, width: 200, height: 200)
            break

        case false:
            let yesAction = UIAlertAction(title: "Yes", style: .default, handler: setUpHandler)
            let noAction = UIAlertAction(title: "no", style: .default, handler: setUpHandler)
            actions.append(yesAction)
            actions.append(noAction)
            rect = CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY, width: 0, height: 0)
            break
    }

    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

    alert.popoverPresentationController?.sourceView = self.view
    alert.popoverPresentationController?.sourceRect = self.view.bounds
    for action in actions {
        alert.addAction(action)
    }
    let popover = alert.popoverPresentationController
    popover?.delegate = self
    popover?.sourceView = view

    DispatchQueue.main.async {
        self.present(alert, animated: true, completion: nil)
    }
}

func setUpHandler (alert: UIAlertAction) {

    print ("received: \(String(describing: alert.title))")
    switch alert.title {
    case "Yes":
        print("show set up")
    case "No":
        print("show set up")
    default:
        print("show set up")
    }
}

只创建您在案例中需要的内容。因此,设置一些变量(我称之为设置工作)来确定每个案例的变化。然后,在案例之后完成所有工作后,显示警报。

关于您的问题 -- 您应该让每个按钮都有不同的处理程序。它 1) 最易读,2) 在不那么耦合的地方提供它。允许多次使用。