gpt4 book ai didi

swift - 如何使警报中的取消按钮取消操作?

转载 作者:行者123 更新时间:2023-11-30 12:14:16 25 4
gpt4 key购买 nike

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

我的目标是执行“NO”操作,取消该操作,这样输入就不会被添加。你能告诉我怎么做吗?

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

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

}

最佳答案

只需将添加项目的代码放入 OK 闭包即可:

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.
if (input.text != "")
{
list.append(input.text!)
input.text = ""
}
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)
}
}

关于swift - 如何使警报中的取消按钮取消操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45621078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com