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

  • SEARCH
  • Home
  • 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 6691935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:48:34+00:00 2026-05-26T05:48:34+00:00

I have an action sheet with options that vary depending on the circumstances. There

  • 0

I have an action sheet with options that vary depending on the circumstances. There are enough different button titles that I would like to construct an array of those button titles first, but I can’t figure out how to convert that into the varargs format.

I want to do something like this:

NSMutableArray *buttonTitles = [NSMutableArray array];
if (condition1) {
    [buttonTitles addObject: @"Do action 1"];
}
if (condition2) {
    [buttonTitles addObject: @"Do action 2"];
}
if (condition3) {
    [buttonTitles addObject: @"Do action 3"];
}
if (condition4) {
    [buttonTitles addObject: @"Do action 4"];
}
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease];

Now obviously if I had to I could do something like this instead:

UIActionSheet *actionSheet = nil;
if (condition1 && condition2 && condition3 && condition4) {
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease];    
} else if (condition1 && condition2 && condition3 && !condition4) {
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease];    
} 
// else ...
// about 14 other cases!

But that would be horrible. Anyone know some nice syntactic sugar to help me out?

EDIT:
It has been suggested that I use addButtonWithTitle, which on the face of it looks great, unfortunately it this puts the additional buttons after the cancel button, which isn’t desirable.

I believe this is bug with Apple’s code since their documentation on addButtonWithTitle states:

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized.

HI requirements (Apple’s own Human Interface guidelines) favor the cancel button below all other options, so I’d say Apple screwed up. Of course, that doesn’t really help me, so I’m back to trying to convert between an NSArray and a varargs, which I still don’t know how to do.

  • 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-05-26T05:48:34+00:00Added an answer on May 26, 2026 at 5:48 am

    You can try this

    NSMutableArray *buttonTitles = [NSMutableArray array];
    if (condition1) {
        [buttonTitles addObject: @"Do action 1"];
    }
    if (condition2) {
        [buttonTitles addObject: @"Do action 2"];
    }
    if (condition3) {
        [buttonTitles addObject: @"Do action 3"];
    }
    if (condition4) {
        [buttonTitles addObject: @"Do action 4"];
    }
    [buttonTitles addObject: @"Cancel"];
    UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease];
    
    for (NSString *title in buttonTitles) {
        [actionSheet addButtonWithTitle: title];
    }
    
    [actionSheet setCancelButtonIndex: [buttonTitles count] - 1];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a action result method i would like to run. How can i
I have an Action Sheet that is called from my root view controller using
I have an iPhone app that uses an action sheet but I can't work
I have an action sheet that is causing me grief on the iphone in
I have the following code that is called when a button on an action
I have read on another post ( how to dismiss action sheet ) that
I have an action sheet which works like this: - (void)Method1 { UIActionSheet *photoSourceSheet=[[UIActionSheet
I would like to know how to present the Open In... Action Sheet (iPhone)
I have an action inside my controller class and I want two different routes
I have some code that works and changes the style sheet using a form.

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.