繁体   English   中英

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)在耦合程度不高的地方提供的。 允许多种用途。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM