Reputation: 12093
I have a UIActionSheet that I created Dynamically and it can have a number of buttons from 1 to 5 including cancel button. I can get the cancel button to work fine but lets say that only 2 buttons are need to be added onto the UIActionSheet, lets say they buttons 2 and 4 how do I determine which action to carry out. Cause normally I would use something like.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch(buttonIndex) {
case 0:
// Do something here.
break;
case 1:
// Do something else here.
break;
case 2:
// Do something else again.
break;
case 3:
// Do something else here again.
break;
default:
break;
}
But if I only add two buttons dynamically there only going to use cases 0, 1. But if I add button 4 as one of the two buttons I still want it to use case 3. Is this possible or is there another way of doing it?
Upvotes: 2
Views: 1418
Reputation: 2573
When you create the UIActionSheet, keep a separate array mapping your button indexes to the actual tasks you want to perform (an actions enumeration, for example).
Then switch on tasks[buttonIndex]
instead of buttonIndex
.
Upvotes: 2
Reputation: 135550
Use the buttonTitleAtIndex:
method to check against the buttons' titles. You have to take care that your code won't break if you change a button's title in one place, though. It's probably best to save each button's title in an extra variable when you create the buttons and use this variable to compare with the button titles later.
Upvotes: 5