-1

How to perform a segue only if a condition happens?

What I have tried:

  • In Main.storyboard I have two view controllers. I have control drag from the View Controller icon of the ViewController.swift to anywhere in the SecondViewController.swift.

  • I select the arrow with the segue and In Attributes > Identifier I give it the name segueTo2

  • In ViewController.swift I have a textField1 and the button toTo2. All connected in the storyboard. I also have:

    @IBOutlet weak var textField1: UITextField!  
    
    @IBAction func goTo2(_ sender: UIButton) {
      performSegue(withIdentifier: "segueTo2", sender: self)
    }
    
    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    
    let str: String? = textField1.text
    if str!.isEmpty {
         print ("text field is empty. Do not do the segue")
         return false
     }
     else {
         return true
     }
    

    }

The same code works well in macOS (only changing textField1.text for textField1.stringValue). In iOS it does not work. That is, when textField1 is empty it still makes the segue.

How to make the segue only when the Text Field has content?

Nrc
  • 9,577
  • 17
  • 67
  • 114
  • Why the negative point. I have searched extensively and no one explains that. What do I need to do to improve the question? – Nrc May 04 '20 at 11:29

2 Answers2

1

Simple solution, it's good practice to check the condition directly right before calling performSegue

@IBAction func goTo2(_ sender: UIButton) {
    guard !textField1.text!.isEmpty else { return }
    performSegue(withIdentifier: "segueTo2", sender: self)
}

The delegate method is actually only for the hard-connected segues, so you can delete it

  override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

    let str: String? = textField1.text
    if str!.isEmpty {
          print ("text field is empty. Do not do the segue")
         return false
     }
     else {
         return true
     }
}  

vadian
  • 274,689
  • 30
  • 353
  • 361
  • That is right with iOS. Why not with macOS? As I said, if I only use the if or guard in macOS, it makes the segue even when text field is empty. It works wll if I use all the code that I mention, including the shouldPerformSegue – Nrc May 04 '20 at 14:22
0

This answer explains what's happening with your logic.

What you can do instead, check if the textfield is filled inside the button function before performing the segue.

Ali
  • 497
  • 1
  • 4
  • 16
  • This is even more confusing! Now I realize that in iOS if I put the if-else in the button I do not need the shouldPerformSegue. In macOS I need the shouldPerformSegue and I have to put the if-else there. – Nrc May 04 '20 at 10:35
  • Please mark as answer if I helped solving your problem – Ali May 04 '20 at 10:39
  • This helps but it does not solve the problem. The answer suggested does not explain my case. – Nrc May 04 '20 at 11:00
  • Do you mean shouldPerformSegue is not necessary in iOS? Why it is necessary in macOS? – Nrc May 04 '20 at 11:27
  • When you call performSegueWithIdentifier yourself in code, the shouldPerformSegue is not called. – Ali May 04 '20 at 11:39
  • That is right in iOS, but not in macOS, I think. I have tried several times. Am I missing anything? – Nrc May 04 '20 at 11:49