如何在警报中取消按钮取消操作?

时间:2017-08-10 18:29:45

标签: swift alert

我已经发出警报,但是应用程序会添加项目,无论我是否点击“否”按钮,当警报弹出时,“是,我确定”按钮。

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

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)
    }
}

编辑:

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

导入UIKit

类Sec​​ondViewController: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)
}

}

1 个答案:

答案 0 :(得分:0)

点击&#34时,您没有附加该项目;是的,我确定&#34;按钮。删除 @IBAction func addItem(_ sender:Any)方法中的以下代码,并将其放在 OKAction 处理程序块中。

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(title:String,message:String)(在此处添加代码)

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 = ""
    }
}