2017-08-10 90 views
0

我已經提醒你了,但是當應用程序彈出警告時,無論點擊「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) 
} 

}

+0

我不清楚地看到你的要求。但是,如果您不向取消操作關閉添加任何代碼,則不會發生任何事情。 :) –

+0

對不起,如果我不夠清楚。在這個編程的新事物。 :)現在我有一個列表。我可以添加項目,我在文本字段中鍵入列表/表格視圖,當我按下按鈕。但是當我按下按鈕時,我想要一個警報彈出,並詢問我是否確定要添加此項目。現在警報彈出,但是如果我按下取消按鈕,就會發生同樣的情況,就好像我按了OK按鈕。 我想取消/「否」按鈕取消操作,所以如果按「否」,文本WONT將被添加到列表/表格視圖中。 :) –

回答

0

只是簡單地把代碼添加項目到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) 
} 
} 
+0

非常感謝。有效! ;) –

0

您要添加的input.text反正你addItem(_ :)方法顯示您UIAlertController後。

所以,如果你想避免input.text總是隻有添加時確定按鈕被竊聽你應該包括在封閉的行動,當你創造了它和的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(標題:字符串消息:字符串)(把這裏附加代碼)

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

非常感謝。有效! ;) –