Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:52:58+00:00 2026-06-13T10:52:58+00:00

I am trying to find an elegant solution to UIActionSheet problem. I use UIActionSheets

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:52:59+00:00Added an answer on June 13, 2026 at 10:52 am

    Since I created a block-based version of UIAlertView and UIActionSheet, I personally never use the delegate-based Apple version again.
    You can download my OHActionSheet and OHAlertView classes 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).

    NSArray* otherButtons = @[ @"aaa", @"bbb", @"ccc", @"ddd" ];
    [OHActionSheet showSheetInView:self.view
                             title:nil
                 cancelButtonTitle:@"cancel"
            destructiveButtonTitle:@"erase"
                 otherButtonTitles:otherButtons
            completion:^(OHActionSheet* sheet, NSInteger buttonIndex)
     {
       if (buttonIndex == sheet.cancelButtonIndex) {
         // cancel
       } else if (buttonIndex == sheet.destructiveButtonIndex) {
         // erase
       } else {
         NSUInteger idx = buttonIndex - sheet.firstOtherButtonIndex;
         // Some magic here: thanks to the blocks capturing capability,
         // the "otherButtons" array is accessible in the completion block!
         NSString* buttonName = otherButtons[idx];
         // Do whatever you want with idx and buttonName
       }
     }];
    

    Additional Note: how to switch/case on NSStrings

    Note that in the otherButtons part of the if/else test in your completion handler, you can then either test for the idx using a switch/case, or use my ObjcSwitch category, that will allow you to write switch/case-like code but for NSStrings, so you can have a code like this in your OHActionSheet‘s completion handler:

    NSUInteger idx = buttonIndex - sheet.firstOtherButtonIndex;
    NSString* buttonName = otherButtons[idx];
    [buttonName switchCase:
       @"aaa", ^{ /* Some code here to execute for the "aaa" button */ },
       @"bbb", ^{ /* Some code here to execute for the "bbb" button */ },
       @"ccc", ^{ /* Some code here to execute for the "ccc" button */ },
       ..., nil
    ];
    

    EDIT : Now that the latest LLVM compiler supports the new “Object Literals” syntax, you can do the same as ObjcSwitch using the compact syntax of an NSDictionary:

    ((dispatch_block_t)@{
       @"aaa": ^{ /* Some code here to execute for the "aaa" button */ },
       @"bbb": ^{ /* Some code here to execute for the "bbb" button */ },
       @"ccc": ^{ /* Some code here to execute for the "ccc" button */ },
    }[buttonName] ?:^{
           /* Some code here to execute for defaults if no case found above */
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve the following problem but cannot find an elegant solution.
I've looked around, and I'm trying to find an elegant solution to this, and
Trying to find an elegant solution for a problem in my CMS system. If
I'm trying to find an elegant OOP solution for the following problem. Assume we
I have been trying for about an hour now to find an elegant solution
I've spent some time trying to find an elegant and cross browser solution but
I am trying to find a slightly more elegant solution to find if a
Trying to find an elegant solution to reset a webform (or more specifically two
I am trying to find an elegant solution in the form of a SQL
I'm trying to find some kind of elegant solution for fading in/out a TextView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.