【问题标题】:Action on Return Key Tap in Alert警报中的返回键点击操作
【发布时间】:2015-05-13 19:58:14
【问题描述】:

如何捕获返回键按下并为警报内的文本字段执行操作? 这是警报的代码:

    var alert = UIAlertController(title: "Add Country", message: "Add a country to the Speaker's List", preferredStyle: .Alert)

    let saveAction = UIAlertAction(title: "Save", style: .Default) {
        (action: UIAlertAction!) -> Void in
        let textField = alert.textFields![0] as! UITextField
        self.countries.append(textField.text)
        self.speakersListTableView.reloadData()
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .Default) {
        (action: UIAlertAction!) -> Void in
    }

    alert.addTextFieldWithConfigurationHandler {
        (textField: UITextField!) -> Void in
    }

    alert.addAction(saveAction)
    alert.addAction(cancelAction)

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您需要设置文本字段的委托并在您的类中实现 UITextFieldDelegate。像这样:

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate
    {
        override func viewDidLoad()
        {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func viewDidAppear(animated: Bool)
        {
            var alert = UIAlertController(title: "Add Country", message: "Add a country to the Speaker's List", preferredStyle: .Alert)
    
            let saveAction = UIAlertAction(title: "Save", style: .Default) {
                (action: UIAlertAction!) -> Void in
                let textField = alert.textFields![0] as! UITextField
                //self.countries.append(textField.text)
                //self.speakersListTableView.reloadData()
            }
    
            let cancelAction = UIAlertAction(title: "Cancel", style: .Default) {
                (action: UIAlertAction!) -> Void in
            }
    
            alert.addTextFieldWithConfigurationHandler {
                (textField: UITextField!) -> Void in
                textField.delegate = self
            }
    
            alert.addAction(saveAction)
            alert.addAction(cancelAction)
    
            self.presentViewController(alert, animated: true) { () -> Void in
            }
        }
    
    
        func textFieldDidBeginEditing(textField: UITextField)
        {
            println("textFieldDidBeginEditing")
        }
    
        func textFieldDidEndEditing(textField: UITextField) {
            println("textFieldDidEndEditing")
        }
    
    }
    

    【讨论】:

    • 谢谢!如何让 textFieldDidEndEditing 函数关闭在 viewDidAppear 中打开的警报?
    • 另外,我希望 textFieldDidEndEditing 函数与我的 saveAction 函数执行相同的操作,但是当触发 Save 操作时,我不会完成双倍的工作。有什么建议么?再次感谢!
    • 看看这个问题的答案,我想这就是你要找的:stackoverflow.com/a/25628065/341994
    • 不完全(至少,我不这么认为)。问题真的是:如果按下警报按钮,我怎样才能阻止 textFieldDidBeginEditing 执行?
    • 听起来完全不同,您可能想将其作为另一个问题发布。
    猜你喜欢
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2017-06-07
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多