2017-08-10 96 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

非常感谢。有效! ;) –