What I'm trying to do:
- checking a condition, if the condition is true, perform segue as normal. If condition is false, call the shouldPerformSegue method and return false to cancel the segue.
How I'm trying to do this:
func buttonTapped{
if(condition is true){
// Perform actions
// Perform segue as normal
} else{
shouldPerformSegue(withIdentifier "mySegueIdentifier", sender: self){
return false
}
}
}
Errors I am receiving:
- extra argument in call. I removed the sender parameter to try and get this to work, and it got rid of the errors for me, but the segue still performed when it shouldn't.
I searched for the correct way to use this method in the apple documentation, but I am still unsure how to do so. Any help will be gratefully accepted and highly appreciated.
If you want to perform logic to decide if a segue should be "performed" or not, you need to override
shouldPerformSegue
. This will allow the OS to (1) kick off things properly and (2) for you to be able to decide if the segue gets done.First of all, you’re attempting to provide a closure as the third parameter to the method, which doesn’t take three parameters, doesn’t take a closure as a parameter at all and this method shouldn’t be called by your code at all.
Second, implement some sort of Boolean flag ‘isSegueActive’ which would be set to ‘false’ or ‘true’ depending on the condition in the related button action.
Third, override ‘shouldPerformSegue’ in your view controller and make it return the value of ‘isSegueActive’.
That way when performing the segue, your view controller will call ‘shouldPerformSegue’ to know whether a segue should be performed.