我想创建一个UIActionSheet
的子类,它使用块而不是委托。所以我创建了以下类:
#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
除了一个问题外,它的工作原理是:我无法为otherButtonTitles
传递多个字符串。如果我按上述方式执行,我(显然)会在方法调度&#34;中找到一个&#34; Missing sentinel。
我的问题是:如何将otherButtonTitles
参数传递给UIActionSheet
的初始值设定项?
答案 0 :(得分:1)
请参阅此great blog post有关可变方法的信息。主要是如何使用
访问变量参数va_list args;
va_start(args, firstArg);
va_arg(args, NSString*)