dparsons
dparsons

Reputation: 2866

More elegant way to handle the Cancel Button click from a UIActionSheet?

I am working on an iOS 5.0 app and I was wondering if there is a more "elegant" way to handle the clicking of the cancel button. There doesn't appear to be anything in the documentation so I am thinking the answer is 'no'.

Basically I have a UIActionSheet that currently has 2 buttons and the Cancel Button. The first two buttons do exactly what they are supposed to do based on the code I have in

 -(void)actionSheet:(UIActionSheet *)actionsheet clickedButtonAtIndex:(NSInteger)buttonIndex

The first two buttons rely on some of the same variables but, depending on which is clicked, they preform different actions. These variables are instantiated outside of my switch statement in the above method and then are acted upon inside of the cases. The cancel button doesn't rely on any of these variables (obviously) so to avoid all of the instantiation I have all of this code wrapped in an if statement that tests for the Cancel button index.

This is less than ideal. If I add new buttons to the Action Sheet I have to remember to update the if statement. The code itself looks messy in general. I could write a new method to handle the click logic of the first two buttons but didn't want to go that far if there was a way to simply dismiss the ActionSheet via the cancel button that prevents the clickedButtonAtIndex method from being called.

So that is my question: is there a way to just dismiss the ActionSheet without that method being called?

Upvotes: 0

Views: 903

Answers (1)

George
George

Reputation: 4029

Well , there is something you can do... in the method you mentioned , first thing do this:

if(buttonIndex == actionSheet.cancelButtonIndex)
     return;

As far as I know it's the best you can do.

Hope this helps.

Regards,

George

Upvotes: 0

Related Questions