我想要一个子类 UIActionSheet
使用 block 方法而不是委托(delegate)。
我的问题是当我在 UIActionSheet
上调用 super 初始化时可变参数 ...
在方法结束时不被识别为 va_list
并且操作表只显示第一个按钮。
这里类实现.m
@interface FLActionSheet ()
@property (nonatomic,strong) actionClickedBlock clickedBlock;
@end
@implementation FLActionSheet
+ (id)actionSheetWithTitle:(NSString *)title
clickedBlock:(actionClickedBlock)clickedBlock
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
return [[self alloc] initWithTitle:title
clickedBlock:clickedBlock
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:otherButtonTitles];
}
- (id)initWithTitle:(NSString *)title
clickedBlock:(actionClickedBlock)clickedBlock
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [super initWithTitle:title
delegate:self
cancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitles:otherButtonTitles,nil];
if (self)
{
self.clickedBlock = [clickedBlock copy];
}
return self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
self.clickedBlock(buttonIndex);
}
@end
在这里我如何初始化操作表:
[[[FLActionSheet alloc] initWithTitle:@"Ordina per..."
clickedBlock:^(NSInteger buttonIndex) {
switch (buttonIndex)
{
case 0:
break;
default:
break;
}
}
cancelButtonTitle:nil
destructiveButtonTitle:@"Annulla"
otherButtonTitles:@"Data crescente", @"Data decrescente", @"Mittente crescente", @"Mittente decrescente"]
showFromBarButtonItem:myBarButtonItem
animated:YES];
结果如下:
我肯定做错了什么,但我不明白是什么。
想法?
最佳答案
UIActionSheet
是 不是 设计为 子类 . initWithTitle:delegate:cancelButtonTitle:...
是 不是 指定初始化器,它是使用 [self init]
实现的有以下 setTitle:
和 addButtonWithTitle:
来电。你应该这样做。 va_list
及相关功能。 Implementation here.然后在他们每个人上调用addButtonWithTitle:
如我所说。 关于ios - 如何正确子类化 UIActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167890/