Alert message fails to appear

devil2pay

I've been trying to get an alert message to pop up when a user touches "Start" to start a timer when any or all of the 3 required textfields are empty. No alert message pops up in the simulator.

@IBAction func start(_ sender: Any) {
//the following code produces an alert if any of the specified text fields are blank when the "start" button is pressed
if medName.text == nil || dose.text == nil || freq.text == nil {

    let alertController = UIAlertController(title: "Field(s) must be filled in!", message: "Please complete all fields.", preferredStyle: UIAlertControllerStyle.alert)

    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action)
        in
        self.dismiss(animated: true, completion: nil)}))

    self.present(alertController, animated: true, completion: nil)

    } else {

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.decreaseTimer), userInfo: nil, repeats: true)

    let itemsObject = UserDefaults.standard.object(forKey: "cellContent")

    var cellContent:[String]

    if let tempItems = itemsObject as? [String] {

        cellContent = tempItems

        cellContent.append(medName.text!)

    } else {

        cellContent = [medName.text!]

    }

    UserDefaults.standard.set(cellContent, forKey: "cellContent")
    //the following code clears the text within each of the specified text fields when start is pressed
    medName.text = ""
    dose.text = ""
    freq.text = ""
    }
}
Rashwan L

A text field is not nil when it´s empty and has no text, that´s why you don´t enter the if-statement. Do this check instead where you check if your text is "":

if medName.text == "" || dose.text == "" || freq.text == ""

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related