【问题标题】:How to make the cancel button in alert cancel the action?如何使警报中的取消按钮取消操作?
【发布时间】:2017-08-10 18:29:44
【问题描述】:

如您所见,我发出了警报,但无论我在弹出警报时是否单击“否”按钮或“是的,我确定”按钮,应用程序都会添加该项目。

我的目标是做出“否”动作,取消动作,这样输入就不会被添加。你能告诉我怎么做吗?

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

}

【问题讨论】:

  • 我看不清楚你在问什么。但如果你不向取消操作闭包添加任何代码,则不会发生任何事情。 :)
  • 对不起,如果我不够清楚。这个编程的新东西。 :) 现在我有一个清单。当我按下按钮时,我可以添加项目,我在列表/表格视图中输入文本字段。但是当我按下按钮时,我希望弹出一个警报,并询问我是否确定要添加此项目。现在警报正在弹出,但如果我按下取消按钮也会发生同样的情况,就像我按下确定按钮一样。我想要取消/“否”按钮来取消操作,所以如果我按“否”,文本不会被添加到列表/表格视图中。 :)

标签: swift alert


【解决方案1】:

只需将添加项目的代码放到 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)
}
}

【讨论】:

    【解决方案2】:

    在显示UIAlertController 之后,无论如何您都在addItem(_ :) 方法中添加input.text

    因此,如果您想避免始终添加 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 = ""
            }
    }
    

    对于取消按钮,您不需要任何操作关闭,除非您也想在点击取消按钮时执行某些操作。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      您没有在单击“是的,我确定”按钮时附加该项目。删除 @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?")
      
      
      }
      

      Inside 方法:func createAlert(title:String, message: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 = ""
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多