14 Replies
      Latest reply on Apr 17, 2019 9:43 PM by Claude31
      Joel14 Level 1 Level 1 (0 points)

        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)
                }
        
        • Re: Prevent Segue if Empty Text Field
          OOPer Level 7 Level 7 (4,875 points)

          Please show more context. Have you placed the segue from the source view controller? Isn't it from any of UI controls or cells?

          Also you should better show where in your code (which class? which method?) the lines you have shown exist.

          • Re: Prevent Segue if Empty Text Field
            Claude31 Level 8 Level 8 (5,645 points)

            Are you sure it is the

            performSegue(withIdentifier: "Create Account", sender: AnyObject.self)

             

            which is executd.

             

            Add a print just before to check:

             

            } else {
                 print("Will performSegue")
                 performSegue(withIdentifier: "Create Account", sender: AnyObject.self) 
            }
              • Re: Prevent Segue if Empty Text Field
                PBK Level 7 Level 7 (3,105 points)

                What Claude31 said and...

                       ....if it does print "Will performSegue" then add a similar print statement before the Alert.show(s) to be sure that is the line that is showing the Alert.

                 

                As written your code will do what you want it to.  That usually means 'the problem in the code is never where you are looking'.

                • Re: Prevent Segue if Empty Text Field
                  Joel14 Level 1 Level 1 (0 points)

                  I added the will perform segue print. However, when I test it out it does not print yet it still goes to the next screen and shows an error message for the blank text field.

                    • Re: Prevent Segue if Empty Text Field
                      Claude31 Level 8 Level 8 (5,645 points)

                      So you have another call to segue.

                      => Search everywhere for "segue" in your code

                       

                      Or may be you defined in IB ?

                           => Inspect in IB all the arrows going to the next screen

                        • Re: Prevent Segue if Empty Text Field
                          Joel14 Level 1 Level 1 (0 points)

                          I was able to find what was causing it to go to the next screen I had the segue set to show I changed it to custom. What is happening now is when I press the create account button but leave the text fields blank the app just crashes and doesn't show any pop up message.

                            • Re: Prevent Segue if Empty Text Field
                              Claude31 Level 8 Level 8 (5,645 points)

                              Please clarify.

                               

                              Have you defined the segue in IB ? (seems so)

                              Have you a segue AND an IBAction defined for the button ?

                              ==> You should not. You cannot have both.

                              So either

                              - define the segue from yje viewController (not starting from the button) and keep the IBAction as it is

                              - remove the IBAction and put the code for alerting in shouldPerformSegue ; return false in case of alert.

                               

                              And return to show segue

                                • Re: Prevent Segue if Empty Text Field
                                  Joel14 Level 1 Level 1 (0 points)

                                  What I did was I control-dragged from a button in one view controller to another then I selected the custom segue. I also for that button made an action and in that action, I put the code that I had showed earlier when I first had asked the question.

                                    • Re: Prevent Segue if Empty Text Field
                                      OOPer Level 7 Level 7 (4,875 points)

                                      control-dragged from a button in one view controller to another

                                       

                                      That is one thing you should not do when you want to manage the segue programmatically with `performSegue(withIdentifier:sender:)`.

                                       

                                      - Delete the existing segue

                                      - Add new segue control-dragging from the source view controller to another

                                        (Not from a button!)

                                      - Set the identifer of the new segue as the old one "Create Account"

                                      • Re: Prevent Segue if Empty Text Field
                                        Claude31 Level 8 Level 8 (5,645 points)

                                        You should not have both

                                        - a segue like this in IB from button to VC and

                                        - an IBAction.

                                         

                                        As I explained before:

                                        So either

                                        - define the segue from the viewController (not starting from the button) and keep the IBAction as it is

                                        - remove the IBAction and put the code for alerting in shouldPerformSegue ; return false in case of alert.

                                          • Re: Prevent Segue if Empty Text Field
                                            Joel14 Level 1 Level 1 (0 points)

                                            I did as you said by setting the segue from the view controller and keeping the code in the IBAction. But what is happening now is that it shows the pop-up message if there is a blank text field but once all text field is filled in and the user presses the create account button the app crashes.

                                              • Re: Prevent Segue if Empty Text Field
                                                Claude31 Level 8 Level 8 (5,645 points)

                                                Please, post the complete code for the controller.

                                                 

                                                Have you performed a Clean Build Folder after resetting the segues ?

                                                 

                                                How is segue precisely defined now ? Standard Show or Custom ?

                                                What is the exact crash message ?

                                                Check segue identifier, must be exactly the same as in code (including uppercase).

                                                If not matching, you'll get a crash with an error such as (here, account vs Account):

                                                Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<ViewController: 0x7f8bc3d0fff0>) has no segue with identifier 'Create account''

                                                 

                                                Another point: change the sender to self for instance, or sender (the IBAction sender)

                                                performSegue(withIdentifier: "Create Account", sender: self)

                                                 

                                                I would need to look at the rest of controller code to check if that could be the cause of crash

                                • Re: Prevent Segue if Empty Text Field
                                  ManuelMB Level 3 Level 3 (165 points)
                                  func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
                                      guard let firstName = productNameTextField.text, !firstName.isEmpty, firstName.count > 0  else {
                                          firstNameAlert.show()
                                          return false
                                      }
                                      guard let lastName = productNameTextField.text, !lastName.isEmpty, lastName.count > 0  else {
                                          lastNameAlert.show()
                                          return false
                                      }
                                  
                                      guard let email = productNameTextField.text, !email.isEmpty, email.count > 0  else {
                                          emailAlert.show()
                                          return false
                                      }
                                  
                                      guard let addressLine1 = productNameTextField.text, !addressLine1.isEmpty, addressLine1.count > 0  else {
                                          addressAlert.show()
                                          return false
                                      }
                                      return true
                                  }