I am trying to find an elegant solution to UIActionSheet problem.
I use UIActionSheets like this:
UIActionSheet * myChoices = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:@"erase"
otherButtonTitles: @"aaa", @"bbb", @"ccc", @"ddd", nil];
the problem is that in order to discover the option selected by the user, I have to use this:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch ([actionSheet tag]) {
case 0: ...
case 1: ...
case 2: ...
case 3: ...
}
}
this case based on index is terrible, because if I change the order of the aaa, bbb, ccc, etc., on the action sheet I have to change the case order. This index stuff is not good as a solid solution.
I have tried to imagine a way to do that and become index independent but did not come any satisfactory solution. Using buttonTitleAtIndex is not good enough too, because my apps are localized and I would have to test for n titles for every entry. Any suggestions?
Since I created a block-based version of
UIAlertViewandUIActionSheet, I personally never use the delegate-based Apple version again.You can download my
OHActionSheetandOHAlertViewclasses in my GitHub repository.Because they are based on the completionBlock pattern, they are more readable (all the code is at the same place, no common delegate for multiple
UIActionSheets, …), and more powerful (because blocks also capture their context as needed).Additional Note: how to
switch/caseon NSStringsNote that in the otherButtons part of the
if/elsetest in your completion handler, you can then either test for theidxusing aswitch/case, or use myObjcSwitchcategory, that will allow you to writeswitch/case-like code but forNSStrings, so you can have a code like this in yourOHActionSheet‘s completion handler:EDIT : Now that the latest LLVM compiler supports the new “Object Literals” syntax, you can do the same as
ObjcSwitchusing the compact syntax of an NSDictionary: