2017-08-10 12 views
0

私はあなたが見ることができるように警告を出しましたが、私が "いいえ"ボタンをクリックしていても、警告がポップアップしたときに "はい、確信しています"ボタンをクリックしていても、アラートでキャンセルボタンを作成するには、アクションをキャンセルしますか?

私の目標は「いいえ」アクションを作成し、アクションをキャンセルして入力が追加されないようにすることです。あなたは私にどのように教えてくれますか?

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

}

+0

私はしないでくださいあなたが求めていることを明確に見てください。しかし、キャンセルアクションクロージャにコードを追加しないと、何も起こりません。 :) –

+0

申し訳ありませんが私は十分に明確でない場合。このプログラミングのことでは新しい。 :)今、私はリストを持っています。私は、ボタンを押すと、項目を追加することができます。私は、リスト/テーブルビューにテキストフィールドを入力しました。しかし、私はボタンを押したときに私はポップアップに警告が欲しいと私はこの項目を追加すると確信しているか尋ねる。今はアラートがポップアップしていますが、[キャンセル]ボタンを押すと、okボタンを押すように同じことが起こります。 キャンセル/ "NO"ボタンでアクションをキャンセルしたいので、 "NO"を押すと、リスト/テーブルビューにテキストWONTが追加されます。 :) –

答えて

0

は単純にOK閉鎖に項目を追加するためのコードを置く:あなたのUIAlertControllerを表示した後、あなたはあなたのaddItem(_ :)方法で、とにかくinput.textを追加している

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) 
} 
} 
+0

ありがとうございます。出来た! ;) –

0

ですから、input.textを避けたい場合は、常に追加され、OKボタンがタップされている場合にのみ、あなたがそれを作成したときにアクションの閉鎖に含めるとUIAlertController

let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { [weak self] _ in 

     guard let selfStrong = self else { 
      return 
     } 

     // Code in this block will trigger when OK button tapped. 
     if (selfStrong.input.text != "") { 
      selfStrong.list.append(input.text!) 
      selfStrong.input.text = "" 
     } 
} 
のプレゼンテーションの後、それを削除する必要があります

キャンセルボタンの場合、キャンセルボタンがタップされたときに何かしたい場合を除き、アクションクロージャは必要ありません。

私はこれがあなたを助けてくれることを願っています。

+0

ありがとうございます。出来た! ;) –

0

「はい、確信しています」ボタンをクリックしてもアイテムが追加されていません。 @IBAction func addItem(_ sender:Any)メソッド内の以下のコードを削除し、OKActionハンドラブロック内に配置します。

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(タイトル:文字列、メッセージ:String)を(ここにコードを追加置く)

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

ありがとうございます。出来た! ;) –

関連する問題