Skip to content
Browse files

added more generic alert constructor using variadic macros for button…

… names
  • Loading branch information...
1 parent e4e0621 commit de7f6f5ddabe7449e23f0f7bf581504bc4db36c8 @ettore committed Nov 15, 2012
Showing with 28 additions and 50 deletions.
  1. +2 −9 CLCGAlertView.h
  2. +26 −41 CLCGAlertView.m
View
11 CLCGAlertView.h
@@ -38,19 +38,12 @@
message:(NSString *)m
block:(void (^)(NSInteger btn))block;
-- (id)initWithTitle:(NSString *)t
- message:(NSString *)message
- block:(void (^)(NSInteger btn))clicked_btn_block
- cancelButtonTitle:(NSString *)cancelButtonTitle
- submitButtonTitle:(NSString *)submitButtonTitle;
-// otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
-
-(id)initWithTitle:(NSString *)t
message:(NSString *)m
block:(void (^)(NSInteger btn))block
cancelButtonTitle:(NSString *)cancel_btn_title
- submitButtonTitle:(NSString *)submit_btn_title
- otherButtonTitle:(NSString *)other_btn_title;
+ otherButtonTitles:(NSString *)other_btn_titles, ... NS_REQUIRES_NIL_TERMINATION;
+
#endif
@end
View
67 CLCGAlertView.m
@@ -57,57 +57,42 @@ -(id)initWithTitle:(NSString *)t
}
-//
-// TODO: implement variadic arguments handling
-// http://stackoverflow.com/questions/205529
-//
-(id)initWithTitle:(NSString *)t
- message:(NSString *)m
- block:(void (^)(NSInteger btn))block
- cancelButtonTitle:(NSString *)cancel_btn_title
- submitButtonTitle:(NSString *)submit_btn_title
+ message:(NSString *)m
+ block:(void (^)(NSInteger btn))block
+ cancelButtonTitle:(NSString *)cancel_btn_title
+ otherButtonTitles:(NSString *)other_btn_title, ... NS_REQUIRES_NIL_TERMINATION
{
-// va_list ap;
-// int i, sum;
-//
-// va_start (ap, count); /* Initialize the argument list. */
-// sum = 0;
-// for (i = 0; i < count; i++)
-// sum += va_arg (ap, int); /* Get the next argument value. */
-//
-// va_end (ap); /* Clean up. */
-
- self = [super initWithTitle:t
- message:m
- delegate:self
- cancelButtonTitle:cancel_btn_title
- otherButtonTitles:submit_btn_title, nil];
+ va_list args;
+ va_start(args, other_btn_title);
+ NSMutableArray *buttons = [[NSMutableArray alloc] init];
+ id arg = nil;
- if (self) {
- mBlock = [block copy];
+ while ((arg = va_arg(args,id))) {
+ [buttons addObject:arg];
}
-
- return self;
-}
+ va_end(args);
--(id)initWithTitle:(NSString *)t
- message:(NSString *)m
- block:(void (^)(NSInteger btn))block
- cancelButtonTitle:(NSString *)cancel_btn_title
- submitButtonTitle:(NSString *)submit_btn_title
- otherButtonTitle:(NSString *)other_btn_title
-{
- self = [super initWithTitle:t
- message:m
+ self = [super initWithTitle:t
+ message:m
delegate:self
- cancelButtonTitle:cancel_btn_title
- otherButtonTitles:submit_btn_title,other_btn_title,nil];
-
+ cancelButtonTitle:cancel_btn_title
+ otherButtonTitles:nil];
+
if (self) {
+ if (other_btn_title) {
+ [self addButtonWithTitle:other_btn_title];
+ }
+ for (NSString *title in buttons) {
+ [self addButtonWithTitle:title];
+ }
mBlock = [block copy];
}
-
+
+ // cleanup
+ [buttons release];
+
return self;
}

0 comments on commit de7f6f5

Please sign in to comment.
Something went wrong with that request. Please try again.