I’m trying to use a switch statement to read out what button has been clicked in a UIActionSheet (programming for iPhone).
The following is in my FirstViewController.m:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
int countarray=[areaselectArray count];
switch (buttonIndex) {
case countarray: //cancel button has been pressed
//do stuff
break;
default:
area = [areaselectArray objectAtIndex:(buttonIndex)];
[[NSUserDefaults standardUserDefaults] setObject:area forKey:@"mulValue"];
[areaselectArray release];
NSLog(@"Released areaselectArray");
break;
}
}
The buttons for the UIActionSheet are built from an array that I built earlier (and have not yet released). I’m placing my Cancel button at the end of the list by using
int countarray=[areaselectArray count];
[areaselect.cancelButtonIndex = countarray;]
earlier on when allocating my UIActionSheet. Since the amount of buttons changes depending on the amount of entries in the array, I’d like the “Cancel” button to simply dismiss the UIActionSheet, but in all other instances have the Switch statement write the value of the clicked button to “mulValue” in standardUserDefaults.
Is there any way to do this? My main issue right now of course is that a switch function won’t take a variable (like countarray in my example). Is there any way to write the value to a constant (?) before entering the switch statement?
Thanks in advance!
Why don’t you use if-statement instead of switch-statement? You can use cancelButtonIndex property for detecting pressing cancel button.
Also, you are able to use title string for comparison of buttons. However, button titles might be localized. Be careful.