2

How can I capture a return key press and perform an action for a text field inside an alert? Here's the code for the alert:

    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)

1 Answers1

0

You need to set the delegate of the textfield and implement the UITextFieldDelegate in you class. Like this:

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

}
Bryan Cimo
  • 1,268
  • 1
  • 19
  • 34
  • Thanks! How can I have the textFieldDidEndEditing function close the alert opened in viewDidAppear? – Khalil Mair May 15 '15 at 02:48
  • Also, I want the textFieldDidEndEditing function to do the same thing as my saveAction function, but I don't the work done double when the Save action is triggered. Any suggestions? Thanks again! – Khalil Mair May 15 '15 at 03:26
  • Check out the answer for this question, I think it's what you are looking for: http://stackoverflow.com/a/25628065/341994 – Bryan Cimo May 15 '15 at 15:57
  • Not quite (at least, I don't think so). The question is really: how can I stop textFieldDidBeginEditing from executing if the alert buttons are pushed? – Khalil Mair May 18 '15 at 00:48
  • Sounds different enough that you might want to post it as another question. – Bryan Cimo May 18 '15 at 15:32