shouldPerformSegue

Hi, I'm still learning swift and I have 2 questions:

  1. Why is this not working?

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

        if segueSwitch.isOn {

            print("It is on")             //super.shouldPerformSegue(withIdentifier: identifier, sender: Any?.self)

            return true

        } else {

            return false         }     }

My switch is on, so according to to the documentation I called the function under IBAction button with shouldPerformSegue(withIdentifier: "Yellow", sender: nil) Nothing happens, my segue identifier is correct.

  1. As you can see I have a comment written above, do I need to call the super.method in order for the function to have the same ability as the documentation?

Just like how viewDidLoad() has a super.viewDidLoad to maintain the basic view defined in the superclass

No need to call super.

How did you connect the button ?

In addition to the IBAction, have you a direct segue from the button to another view ?

In such a case, shouldPerform is not called.

If so:

  • remove this direct segue from button
  • create a segue from the VC (control drag from the leftmost button at the very top of originating VC in storyboard
  • give it "Yellow" identifier

Hi, I've spent 4 hours racking my brain on this. I've read forums to watching YouTube videos. Idk what is happening, but this is just not working in any capacity. I honestly am second guessing my intelligence but no matter how matter times I'm reading the documentation, I'm literally doing what it says?!?

Could you better instrument code and tell what you get:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        if segueSwitch.isOn {
            print("It is on", identifier)  
            return true
        } else {
            print("It is off", identifier)  
            return false 
       } 
 }

Could you show also code where you:

  • call shouldPerformSegue
  • call performSegue

Did you check the exact name and spelling of segue identifier ?

Could you also show

  • a screenshot of storyboard, with the segue clicked so that we can see where it originates ?
  • the IBAction code for the button
  • the link inspector for the button

For your last question:

Just like how viewDidLoad() has a super.viewDidLoad to maintain the basic view defined in the superclass

Situation is different: viewDidLoad do a lot of initialisations, hence need to do it on super as well so that the subclass inherits it.

In the doc for shouldPerformSegue, no reference to calling on super: Discussion Subclasses can override this method and use it to perform segues conditionally based on current conditions. If you do not implement this method, all segues are performed.

For viewDidLoad, it is not required but considered a good practice:

https://stackoverflow.com/questions/40151723/why-when-do-we-have-to-call-super-viewdidload

They mention a rule of thumb: if the override func does not return a value, you'd better call super.

@Claude31 Thank you for replying! I now have a better understanding of calling super when overriding.

I have attached the images below. I didn't call performSegue at all.

Please ignore the additional button on the left, I deleted stuffs to better show the code.

shouldPerformSegue
 
 
Q