我已经提醒你了,但是当应用程序弹出警告时,无论点击“NO”按钮还是“Yes,I'm sure”按钮,该应用程序都会添加该项目。如何使警报中的取消按钮取消操作?
我的目标是做出“否”的动作,取消动作,这样输入将不会被添加。你能告诉我如何?
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按钮。 我想取消/“否”按钮取消操作,所以如果按“否”,文本WONT将被添加到列表/表格视图中。 :) –