Forward variable arguments for UIAlertView

I am trying to set up a very simple one UIAlertView

with text edit, OK and Cancel buttons, and I want to disable the OK button based on the text edit content.

To keep the delegate from going away before presenting the alert (and thus causing a crash as soon as the user did something with the kind of alert), I subclassed it. Now I want to redirect the argument otherButtonTitles

from my init method to the UIAlertView

init method , but for some reason I just did this:

- (id)initWithTitle:(NSString *)title
           delegate:(id /*<UIAlertViewDelegate>*/)delegate
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ... {

    if (self = [super initWithTitle:title 
                  otherButtonTitles:otherButtonTitles, nil]) {


adds only the first element of the arguments to the alert view. I found that I can manually add buttons to the alert view using this:

va_list args;
va_start(args, otherButtonTitles);
for (NSString *buttonTitle = otherButtonTitles; buttonTitle != nil; buttonTitle = va_arg(args, NSString*)) {
  [self addButtonWithTitle:buttonTitle];


but then my alertViewShouldEnableFirstOtherButton

delegate method is no longer called with the likely explanation in this post .

So what is the correct way to send my method otherButtonTitles

to the UIAlertView

init method ?


source to share

1 answer

Reduce keystrokes:

NSMutableArray *otherButtonTitles = [NSMutableArray array];
// .. collect varargs into ^^^

#define T(n) ([otherButtonTitles objectAtIndex:n])
#define CASE(n, ...) case n: self = [super initWithTitle:title \
                                                 message:message \ 
                                                delegate:delegate \
                                       cancelButtonTitle:cancelButtonTitle \
                                       otherButtonTitles:__VA_ARGS__, nil]; \

switch ([otherButtonTitles count]) {
    CASE(0, nil);
    CASE(1, T(0));
    CASE(2, T(0), T(1));
    CASE(3, T(0), T(1), T(2));
    // ... repeat until bored ...
    default: @throw @"too many buttons"; // or return nil




All Articles