Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I sent an array of strings into an UIActionSheet varargs init method?
    primarykey
    data
    text
    <p>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. </p> <p>I want to do something like this:</p> <pre><code>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]; </code></pre> <p>Now obviously if I had to I could do something like this instead:</p> <pre><code>UIActionSheet *actionSheet = nil; if (condition1 &amp;&amp; condition2 &amp;&amp; condition3 &amp;&amp; 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 &amp;&amp; condition2 &amp;&amp; condition3 &amp;&amp; !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! </code></pre> <p>But that would be horrible. Anyone know some nice syntactic sugar to help me out?</p> <p><b>EDIT:</b> It has been suggested that I use <code>addButtonWithTitle</code>, which on the face of it looks great, unfortunately it this puts the additional buttons after the cancel button, which isn't desirable. </p> <p>I believe this is bug with Apple's code since their documentation on <code>addButtonWithTitle</code> states: </p> <pre><code>// 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. </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload