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
message:(NSString*)message
delegate:(id /*<UIAlertViewDelegate>*/)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
if (self = [super initWithTitle:title
message:message
delegate:delegate
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles, nil]) {
//stuff
}
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];
}
va_end(args);
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
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]; \
break
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
}
source to share