如何使警报中的取消按钮取消动作? [英] How to make the cancel button in alert cancel the action?

查看:109
本文介绍了如何使警报中的取消按钮取消动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您所看到的,我发出了警报,但是无论警报弹出时是单击否按钮还是是,我确定按钮,应用程序都会添加该项目。

I made an alert as you can see, but the app adds the item, no matter if I'm clicking the "NO" button og the "Yes, I'm sure" button when the alert pops up.

我的目标是执行否操作,取消该操作,以便无论如何都不会添加输​​入。你能告诉我怎么做吗?

My goal is to make the "NO" action, cancel the action so the input won't be added anyway. Can you tell me how to?

import UIKit

class SecondViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var input: UITextField!

    @IBAction func addItem(_ sender: Any)
    {
        createAlert(title: "That's a good grail!", message: "Are you sure you want to add this grail?")

        if (input.text != "")
        {
            list.append(input.text!)
            input.text = ""
        }
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.input.delegate = self
    }

    //HIDE KEYBOARD:
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

    //PRESSES RETURN KEY:
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        input.resignFirstResponder()
        return true
    }

    func createAlert (title:String, message:String)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        //CREATING OK BUTTON

        let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { (action:UIAlertAction!) in

            // Code in this block will trigger when OK button tapped.
            print("Ok button tapped");

        }
        alertController.addAction(OKAction)

        // Create Cancel button
        let cancelAction = UIAlertAction(title: "No!", style: .cancel) { (action:UIAlertAction!) in
            print("Cancel button tapped");
        }
        alertController.addAction(cancelAction)

        // Present Dialog message
        self.present(alertController, animated: true, completion:nil)
    }
}

编辑:

现在的代码看起来像这样,谢谢:

The code looks like this now, thanks:

导入UIKit

SecondViewController类:UIViewController,UITextFieldDelegate {

class SecondViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var input: UITextField!

@IBAction func addItem(_ sender: Any)
{
    createAlert(title: "That's a good grail!", message: "Are you sure you want to add this grail?")

}



override func viewDidLoad()
{
    super.viewDidLoad()

    self.input.delegate = self
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


//HIDE KEYBOARD:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

//PRESSES RETURN KEY:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    input.resignFirstResponder()
    return true
}


func createAlert (title:String, message:String)
{
    let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

    //CREATING OK BUTTON

    let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { (action:UIAlertAction!) in

        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");
        if (self.self.input.text != "")
        {
            list.append(self.input.text!)
            self.input.text = ""
        }

    }
    alertController.addAction(OKAction)

    // Create Cancel button
    let cancelAction = UIAlertAction(title: "No!", style: .cancel) { (action:UIAlertAction!) in
        print("Cancel button tapped");
    }
    alertController.addAction(cancelAction)

    // Present Dialog message
    self.present(alertController, animated: true, completion:nil)
}

}

推荐答案

单击是,我确定按钮后,您将不会追加该项目。删除 @IBAction函数addItem(_ sender:Any)方法中的以下代码,并将其放在 OKAction 处理程序块中。

You are not appending the item on click of "Yes, I'm sure" button. Remove the below code inside @IBAction func addItem(_ sender: Any) method and put it inside OKAction handler block.

if (input.text != "")
    {
        list.append(input.text!)
        input.text = ""
    }

这样做:

@IBAction func addItem(_ sender: Any)
    {
        createAlert(title: "That's a good grail!", message: "Are you sure you want to add this grail?")


}

内部方法: func createAlert(标题:字符串,消息:字符串)(在此处添加附加代码)

Inside method: func createAlert (title:String, message:String) (put append code here)

let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { (action:UIAlertAction!) in
        // Code in this block will trigger when OK button tapped.
        print("Ok button tapped");
    if (input.text != "")
    {
        list.append(input.text!)
        input.text = ""
    }
}

这篇关于如何使警报中的取消按钮取消动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆