如何使警报中的取消按钮取消动作?

编程入门 行业动态 更新时间:2024-10-27 10:33:42
本文介绍了如何使警报中的取消按钮取消动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

如何使警报中的取消按钮取消动作?

本文发布于:2023-11-27 10:37:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   按钮   动作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!