我已經提醒你了,但是當應用程序彈出警告時,無論點擊「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將被添加到列表/表格視圖中。 :) –