Search code examples
swifttextfielduialertcontroller

Retrieve content from textfield in uiAlertController


I have a textfield in an uiAlertController, and I would like to retrieve the content inside to build an object. I wrote this following code :

let alertController = UIAlertController(title: "Ajouter une description", message: "De quoi sagit il ?", preferredStyle: UIAlertControllerStyle.alert)
                    alertController.addTextField { (textField : UITextField) -> Void in
                        textField.placeholder = "Description"
                        self.theDescription = textField.text!
                    }
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
                        self.createArtWithDescription(description: self.theDescription)
                    }
                    
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                    //
                }

To build my object, I have another function "createArtWithDescription", but inside I can't retrieve the content of the uiAlertController textfield.

Thanks by advance


Solution

  • Short answer:

        let okAction = UIAlertAction(title: "OK", style: .default) { (result : UIAlertAction) -> () in
            self.theDescription = alertController.textFields?.first?.text
            self.createArtWithDescription(description: self.theDescription)
        }
    

    Long answer:

    create simple playground for testing and inspect everything during execution:

    import UIKit
    import PlaygroundSupport
    class MyViewController : UIViewController {
    
        private var theDescription:String?
        private var label:UILabel = UILabel()
    
        override func loadView() {
            let view = UIView()
            view.backgroundColor = .white
    
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            label.text = "Hello World!"
            label.textColor = .black
            label.backgroundColor = .red
    
            view.addSubview(label)
            self.view = view
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
    
            let alertController = UIAlertController(title: "Ajouter une description", message: "De quoi sagit il ?", preferredStyle:.alert)
            alertController.addTextField { (textField : UITextField) -> () in
                textField.placeholder = "Description"
                // you cant obtain value here - this block only for config purpose
                self.theDescription = textField.text
            }
            let okAction = UIAlertAction(title: "OK", style: .default) { (result : UIAlertAction) -> () in
                // instead use this approach
                self.theDescription = alertController.textFields?.first?.text
                self.createArtWithDescription(description: self.theDescription)
            }
    
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
            //
        }
    
        func createArtWithDescription(description:String?) {
            DispatchQueue.main.async {
                self.label.text = description
            }
        }
    
    }
    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = MyViewController()
    

    More about UIAlertController