0

i am working at my startscreen (viewDidAppear). At the beginning of the app, there should be an alert with some notice message. This works fine. After you click "ok" at the notice, the next alert should pop up with a text field. In this text field you have to type in a double value. I need this double value to set up a lot of things inside the app, so I need this value outside the function to calculate with it. E.g. The notice message describes how the app works. You click OK. Now the next alert pop up with the text field. For example it asks your height. After you type in your height and click OK the height-value is the main part of different calculations. The problem is now, that my app works with the nil-value before I typed in the double value (e.g. height) These are the snippets from the code.

var iNeedTheDataHere:String?
///Alerts
//Start
    let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
    let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)

      //BeforeScreenLoad
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)

        // Start Alert
        startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
            self.input()
        }))
        self.present(startController, animated: true)
    }

    func input() {
        inputController.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
            let answer = inputController.textFields![0].text
            iNeedTheDataHere = answer
        }
        inputController.addAction(submitAction)
    }

When I set Breakpoints, I see that the value of the inputController is set to nil, before I typed in anything. I guess my mistake has something to do with the handler Sorry for my English Hope somebody can help me

TheAppment
  • 23
  • 5

2 Answers2

0
    let alert = UIAlertController(title: "Alert!!!", message: "Please enter your message ", preferredStyle: .alert)
alert.addTextField(configurationHandler: self.configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction) in
 self.number.text?=""
 MBProgressHUD.hide(for: self.view, animated: true)
 }))
 alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in

    }))
    self.present(alert, animated: true, completion: {
                            })
0

I have made some changes in your code to achieve that:

class ViewController: UIViewController {

    let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
    let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {

        startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
            self.input()
        }))
        self.present(startController, animated: true)

    }

    func input() {

        //Present another alert with textField
        let confirmAction = UIAlertAction(title: "Submit", style: .default) { (_) in
            guard let textFields = self.inputController.textFields,
                textFields.count > 0 else {
                    // Could not find textfield
                    return
            }

            //get your textField
            let answerTF = textFields[0]
            let answer = answerTF.text
            //Get values entered by user
            print(answer)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

        //add your textField here
        inputController.addTextField { (textField) in
            //Set a placeholde for textField
            textField.placeholder = "Answer"
        }

        inputController.addAction(confirmAction)
        inputController.addAction(cancelAction)

        self.present(inputController, animated: true, completion: nil)
    }

}

I have added comment to explain what I am doing.

And your result will be:

enter image description here

You can also check demo project here.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165