Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling performSeguewithIdentifier doesn't call shouldperformseguewithIdentifier

Tags:

ios

swift

segue

I have two view controllers. On view controller1 I have the following:

  • a segue that takes me to viewcontroller2 - this segue is named "showme" and is attached to the viewcontroller
  • an IBAction for a UIButton

In my code I have the following for the button press action

@IBAction func buttonPress(sender: AnyObject) {     println("button pressed")         performSegueWithIdentifier("showme", sender: self) } 

I also have the following method:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {     println("Should performing....")     return true }    

For some reason the shouldPerformSegueWithIdentifier function is never called. If however, I add the segue directly on the UIButton to ViewController2 it is.

I have confirmed that calling it direction within my button action works (see below), but this is not what I understand to be the way it works. The same is true for prepareforSegue..

@IBAction func buttonPress(sender: AnyObject) {     println("button pressed")     if (shouldPerformSegueWithIdentifier("showme", sender: self)){         performSegueWithIdentifier("showme", sender: self)} }  
like image 507
es3dev Avatar asked Nov 15 '14 13:11

es3dev


1 Answers

This behaviour is perfectly natural, for the following reasons:

1) shouldPerformSegueWithIdentifier is used to make sure that a segue that has been set up in Storyboards should be triggered, so it only gets called in the case of Storyboard Segues and gives you the chance to not actually perform the segue.

2) When you call performSegueWithIdentifier yourself, shouldPerformSegueWithIdentifier is not called because it can be assumed that you know what you are doing. There would be no point in calling performSegueWithIdentifier but then return a NO from shouldPerformSegueWithIdentifier.

like image 93
nburk Avatar answered Oct 12 '22 23:10

nburk