Reputation: 6775
If I create two (or more) IBActions from the same button in storyboard, what will be the relative order of their execution? I have a situation like this:
@IBAction func foo(sender: UIButton) {
//does something based on value of variable x
}
@IBAction func bar(sender: UIButton) {
//changes variable x
}
I have read this and it says that IBoutlets are called in alphabetically order but behaviour may change. What is the current behaviour in Swift?
Upvotes: 0
Views: 257
Reputation: 7176
Don't rely on application logic being handled by a single event. The IBAction
for a touchesBegan
will fire before an IBAction
for a touchesEnded
(for example), so you implicitly infer a state: one will occur before the other. This is how you connect multiple actions to a single control.
You should only have one IBAction
per event since it performs only one function: letting you know what the user has done. It is then up to you to decide what to do with that information. If you could specify the order the actions were executed in you might as well write a wrapper function that executes in that order and put that in the IBAction
Upvotes: 0
Reputation: 22343
As you said, it could change in the future. Why not just do everything in one buttion-function? There is no difference in using only once action. You can work with if-else, switch statements etc to make sure everything initializes fine. But what you have now is the same as if you would make it like that:
@IBAction func fooBar(sender: UIButton) {
//changes variable x
//does something based on value of variable x
}
Also you shouldn't rely on the fact, that the actions will get called alphabetically at the moment. It can change in future updates and if you rely on that you will have to make many unnecessary changes just to make your code work again.
Upvotes: 1