传递变量参数列表

时间:2011-09-25 12:55:24

标签: iphone objective-c cocoa-touch uiactionsheet objective-c-blocks

我想创建一个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的初始值设定项?

1 个答案:

答案 0 :(得分:1)

请参阅此great blog post有关可变方法的信息。主要是如何使用

访问变量参数
va_list args;
va_start(args, firstArg);
va_arg(args, NSString*)