Pass a variable argument list
I would like to create a subclass of UIActionSheet
that uses blocks instead of delegation. So I created the following class:
#import <UIKit/UIKit.h>
// Public Interface
typedef void (^FJActionSheetCompletion)(int buttonIndex);
@interface FJActionSheet : UIActionSheet
- (id)initWithTitle:(NSString *)title
completionBlock:(FJActionSheetCompletion)completionBlock
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
@end
// Private Interface
@interface FJActionSheet() <UIActionSheetDelegate>
{
FJActionSheetCompletion _completionBlock;
}
@end
// Implementation
@implementation FJActionSheet
- (id)initWithTitle:(NSString *)title
completionBlock:(FJActionSheetCompletion)completionBlock
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [super initWithTitle:title
delegate:self
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:otherButtonTitles];
if (self)
{
_completionBlock = [completionBlock copy];
}
return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
_completionBlock(buttonIndex);
}
@end
This works except for one problem: I can not pass multiple strings for otherButtonTitles
. If I do it as above, I (obviously) get a "Missing sentinel in method dispatch".
My question is: How can I p开发者_JAVA百科ass the otherButtonTitles
argument to the initializer of UIActionSheet
?
see this great blog post about variadic methods. Mainly how to access the variable arguments by using
va_list args;
va_start(args, firstArg);
va_arg(args, NSString*)
精彩评论