私はあなたが見ることができるように警告を出しましたが、私が "いいえ"ボタンをクリックしていても、警告がポップアップしたときに "はい、確信しています"ボタンをクリックしていても、アラートでキャンセルボタンを作成するには、アクションをキャンセルしますか?
私の目標は「いいえ」アクションを作成し、アクションをキャンセルして入力が追加されないようにすることです。あなたは私にどのように教えてくれますか?
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)
}
}
EDIT:
コードは次のようになり、おかげ:
インポートの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ボタンを押すように同じことが起こります。 キャンセル/ "NO"ボタンでアクションをキャンセルしたいので、 "NO"を押すと、リスト/テーブルビューにテキストWONTが追加されます。 :) –