Prevent Segue if Empty Text Field

I am having trouble with my app. The problem I am having is that when I leave a text field empty I want it to have a pop-up message that says empty text field and I want it to stop the segue from moving to the next screen. What is happening is it is showing the pop-up message of the empty text field but it still moves to the next screen. This is the code that I currently have:


   let firstNameAlert : UIAlertView = UIAlertView(title: "Blank Text Field", message: "Please fill in first name.",       delegate: nil, cancelButtonTitle: "OK")
        let lastNameAlert : UIAlertView = UIAlertView(title: "Blank Text Field", message: "Please fill in last name.",       delegate: nil, cancelButtonTitle: "OK")
        let emailAlert : UIAlertView = UIAlertView(title: "Blank Text Field", message: "Please fill in email.",       delegate: nil, cancelButtonTitle: "OK")
        let addressAlert : UIAlertView = UIAlertView(title: "Blank Text Field", message: "Please fill in address.",       delegate: nil, cancelButtonTitle: "OK")
        let phoneFromDefaults = defaults.object(forKey: "Phone Number") as? String
        
        if firstName.text == ""{
            firstNameAlert.show()
        }else if lastName.text == ""{
            lastNameAlert.show()
        }else if email.text == ""{
            emailAlert.show()
        }else if addressLine1.text == ""{
            addressAlert.show()
        }else{
            performSegue(withIdentifier: "Create Account", sender: AnyObject.self)
        }

Accepted Reply

I was able to fix the issue by setting the segue back to push.

Replies

You have probably defined a custom segue.


Then you need to define its class and override the perform method.


Otherwise, just simply use a standard segue.

I was able to fix the issue by setting the segue back to push.