【问题标题】:UIAlertController Button Action in Swift 3Swift 3 中的 UIAlertController 按钮操作
【发布时间】:2017-08-23 17:37:36
【问题描述】:

我想在单击 UIAlertController 的按钮时执行一个操作,我发现了很多关于此的信息,但没有一个对我有用,我得到的更接近的答案是在这个网站上:http://nshipster.com/uialertcontroller/

我实现的代码是这样的:

func success(message: String) {
    let alertSuccess = UIAlertController(title: "Listo", message: message, preferredStyle: UIAlertControllerStyle.alert)
    let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
    alertSuccess.addAction(okAction)
}

func paymentConfirmation(message: String) {
    self.myAlert = UIAlertController(title: "Confirmación", message: message, preferredStyle: UIAlertControllerStyle.alert)
    let myServices = UIAlertAction (title: "Si", style: UIAlertActionStyle.default) { action in
        self.success(message: "Tu pago ha sido exitoso")
    }
    self.myAlert.addAction(myServices)

    let okAction = UIAlertAction (title: "No", style: UIAlertActionStyle.cancel, handler: nil)
    self.myAlert.addAction(okAction)

    self.present(self.myAlert, animated: true, completion: nil)
    return
}

在 paymentConfirmation 函数是我想要显示第二个 UIALertController 的按钮的地方,按钮的名称是 (myServices),所以我添加了我想要该按钮执行的操作,然后我调用该方法在这个区块中:

 self.paymentConfirmation(message: "¿Estás seguro que quieres comprar el siguiente paquete de internet?")

所以,应用程序应该做的是,当用户在第一个按钮 Alert 上单击“Si”时,它应该会出现第二个 Alert,但这没有发生,它什么也没做,有人可以帮助我吗?

【问题讨论】:

    标签: ios swift button uialertcontroller uialertaction


    【解决方案1】:

    您错过了第二个UIAlertController 演示文稿

    func success(message: String) {
        let alertSuccess = UIAlertController(title: "Listo", message: message, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
        alertSuccess.addAction(okAction)
    
       self.present(alertSuccess, animated: true, completion: nil)
    }
    

    【讨论】:

    • 哦,你说得对,这是我的一个小错误,但即使是这样的细节也可以让任何事情发生,哈哈。非常感谢!
    • 欢迎您@ZitaNoriegaEstrada 我很高兴为您提供帮助,祝您编码愉快并致以最诚挚的问候
    猜你喜欢
    • 2017-03-04
    • 2023-03-22
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多