-3

I have a form that needs to be validated before performing a segue and also sending over the data to the next view controller. For now I'm just checking to see if all text fields are filled in:

@IBAction func startBtn(_ sender: Any) {
    if(idInput.text == "" || dob1Field.text == "" || dob2Field.text == "" || dob3Field.text == ""){
        print("no text")
    }
}

My idea is, when the start button is pressed, check if all fields are filled, if they are use prepare to segue to the next VC and send the data.

I'm struggling to understand how to do this, I linked the start button on the storyboard to the VC and gave it an identifier mainUse since it is going to the mainUseController

Here is the prepare function:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == "mainUse"{
        let vc = segue.destination as! mainUseController
    }
}

The part I'm struggling to understand is how to call the prepare function once the check is done and succeeded. Thanks.

  • You don't directly call `prepare(for:sender)`, what you do (after your ifs test), is call `performSegue(withIdentifier:sender:)`. It will call (after checking if it's allowed, like `shouldPerformSegue(withIdentifier:sender:)`) the `prepare(for:sender)`. – Larme Feb 27 '19 at 11:48
  • @Larme that's what I did initially, but I need to pass on the details to the next view controller and performSegue doesn't do that. – badprogramming99 Feb 27 '19 at 11:50
  • So your question is about passing data to the next ViewController through the segue. That's different. Well, in your current code, you have `let vc`, so `vc.someProperty = something`. – Larme Feb 27 '19 at 11:51
  • Yeah, performSegue works as expected but I need the entries from the text fields to be sent over to the next view controller. – badprogramming99 Feb 27 '19 at 11:56
  • Possible duplicate of [IOS - How to segue programmatically using swift](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – El Tomato Feb 27 '19 at 12:27

4 Answers4

1

I linked the start button on the storyboard to the VC and gave it an identifier mainUse since it is going to the mainUseController

Well here is the issue: your segue seems to be generated by dragging from the button to the destination view controller, don't do this because the segue will performed regardless of what's implemented in the button action. Instead drag from the view controller itself (but not from the button) to the destination view controller:

enter image description here control + drag from the view controller to the destination controller

At this point, tapping the button won't forcibly navigates you to the destination view controller. Next what you should do is to perform the segue if the conditions are met, by calling performSegue(withIdentifier:sender:) method:

@IBAction func startBtn(_ sender: Any) {
    if idInput.text == "" || dob1Field.text == "" || dob2Field.text == "" || dob3Field.text == "" {
        print("no text")
        return
    }

    // just don't forget to assign the segue identifier as 'mainUse'...
    performSegue(withIdentifier: "mainUse", sender: nil)
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
0

Use the following code to perform the segue, first check if all condition fulfill then fire segue else display error message accordingly.

@IBAction func startBtn(_ sender: Any) {
        if(idInput.text == "" || dob1Field.text == "" || dob2Field.text == "" || dob3Field.text == ""){
            print("no text")
            //Show alert message here
        }else{
            self.performSegue(withIdentifier: "mainUse", sender: self)
        }
    }
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
0

prepare(for segue:) method is called by the ViewController delegate, you should avoid putting code there that you need to trigger.

What you can call for segueing is performSegue(withIdentifier:sender:)

More in: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621413-performsegue

IMPORTANT CAVEAT

Remember that your outlets are nil when segueing, if you want to assign or pass data to the receiver VC, create a strong property and assign that value before you segue but after the VC instantiation. Labels, texts, etc won't receive any data until they're draw.

If you need the entries from the text fields to be sent over to the next view controller, create a placeholder property and assign it during the segueing process.

You have a nice day!

Helen Wood
  • 1,872
  • 2
  • 26
  • 41
0
@IBAction func startBtn(_ sender: Any) {

if idInput.text?.isEmpty ?? true || dob1Field.text?.isEmpty ?? true || dob2Field.text?.isEmpty ?? true || dob3Fieldtext?.isEmpty ?? true { print("some textField is empty") return } 

dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.performSegueWithIdentifier("YourIdentifier", sender: self)             
    }                                                                          
 }

If you are using navigation controller:

  @IBAction func startBtn(_ sender: Any) {
    if idInput.text?.isEmpty ?? true || dob1Field.text?.isEmpty ?? true || dob2Field.text?.isEmpty ?? true || dob3Fieldtext?.isEmpty ?? true { print("some textField is empty") return } 

    if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourIdentifier") as? NextViewController{
        if let navigator = navigationController {

            navigator.pushViewController(viewController, animated: true)
          }
    }
}