Goktug
Goktug

Reputation: 923

Segue Methods and Their Objectives

When I press the button, I want to initiate new view controller and I want to pass some information into new view controller. This is handled by without any segue methods by using global variables.

However, If I use segue method, Which one of them do I have to use ?

performSegue() or shouldPerformSegue()

Upvotes: 1

Views: 106

Answers (2)

Devang Tandel
Devang Tandel

Reputation: 3008

You have to call

func performSegue(withIdentifier identifier: String, 
           sender: Any?)

The above method will initiate your segue and you can push your next view controller.

For your info

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

The method will return Boolean value, that will specify if to perform segue or not. You have controller over it and you can allow segue to perform or not by return true/false from method.

true - Segue is allowed and will be performed

false - Segue is not allowed and will be aborted.

You can use this method if you want to override any segue you have defined in storyboard and want to perform any other at run time.

Upvotes: 1

André Slotta
André Slotta

Reputation: 14030

You have to use performSegue if you want to initiate a segue programmatically.

With shouldPerformSegue you can add additional logic to decide whether an initiated segue really should happen or not.

Finally in prepareForSegue you can - as the name suggests - prepare the segue (e.g. pass some data to the destination viewcontroller).

Upvotes: 2

Related Questions