1

我想创建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。如果我这样做,我(显然)会得到一个“方法派遣中缺少哨兵”。

我的问题是:如何将otherButtonTitles参数传递给UIActionSheet的初始值设定项?

回答

1

关于各种方法,请参阅great blog post。主要是如何通过使用访问可变参数

va_list args; 
va_start(args, firstArg); 
va_arg(args, NSString*) 
+0

我没有看到它,或者你忘了包含链接? – fabian789

+0

哦是的。支持。 – vikingosegundo

+0

对不起。这是一个漫长的夜晚昨天:) – vikingosegundo