1

with the shouldPerforSegue() func I am able to check some fields before seguing, however, I need it to continue with the segue even if the fields were bad at the time of triggering the segue.

Right now, when the user clicks on a button that triggers a segue, I am checking to see if the images are already uploaded to the server. I display a progress bar along with an alert to indicate the % left. However, right now after the image is uploaded to the server with the progress bar indicating 100%. I still have to manually click "ok" and re-click the seguing button. I am trying to see if there was a way to bypass this hindrance, by just continuing with the segue.

I have tried programmatically triggering the segue button after the image is uploaded, but it does not work as intended.

UIApplication.shared.sendAction(self.saveButton.action!, to: self.saveButton.target, from: nil, for: nil)

The function I have that checks for validation:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        if ((sender as? UIBarButtonItem) != nil) && uploadingImagesToFirebase  {
            // Create the alert controller
            let alertController = UIAlertController(title: "Uploading Images...", message: "", preferredStyle: .alert)
            // Create an ok button
            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
                print("OK")
            }
            alertController.addAction(okAction)

            self.present(alertController, animated: true, completion: {
                //  Add your progressbar after alert is shown (and measured)
                let rect = CGRect(x: alertController.view.frame.width * 0.10, y: alertController.view.frame.height / 2, width: alertController.view.frame.width * 0.80, height: 100)
                self.progressView = UIProgressView(frame: rect)
                alertController.view.addSubview(self.progressView!)
            })

            return false
        }
        return true
    }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // If user clicks on a job cell
        if segue.identifier == "showJobDetails" {
            ...
        }
        else if addJobButton == sender as? UIButton {
            print("Adding a new job from customer view")
            ...
        }
        // If save button was choice selected by user
        else if segue.identifier == "identifier {
            customer.firstName = firstNameTextField.text
            customer.middleName = middleNameTextField.text
            customer.lastName = lastNameTextField.text
            customer.address = addressTextField.text
            customer.email = emailTextField.text
            customer.phone = phoneTextField.text

            customer.jobs = customerJobs

            updateCustomerValuesInFirebase()
            updateJobValuesInFirebase()
        }
    }
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Edward Lim
  • 763
  • 9
  • 31

2 Answers2

1

in Swift3, you have to perform the segue by this line of code:

performSegue(withIdentifier: identifier, sender: sender)

the identifier and sender values are depends on where you call the performSegue, for example if you are in shouldPerformSegue function you can use the function input values, but if you are in other functions you have to add the identifier by yourself(which you set it for the segue in storyboard) and set the sender to nil.

Mina
  • 2,167
  • 2
  • 25
  • 32
1

Before checking (by using the shouldPerformSegue(withIdentifier:sender:)), you should perform it first, you can achieve this by using performSegueWithIdentifier:sender::

Initiates the segue with the specified identifier from the current view controller's storyboard file.

Actually, shouldPerformSegue(withIdentifier:sender:) methods does the following:

Determines whether the segue with the specified identifier should be performed.

So you should do:

performSegue(withIdentifier: "identifier", sender: self)

before checking by using shouldPerformSegue(withIdentifier:sender:).

If you don't know how to set the identifier for the segue, check this answer.

Community
  • 1
  • 1
Ahmad F
  • 30,560
  • 17
  • 97
  • 143