I want subclass a UIActionSheet
to use a block approach instead of delegate.
My problem is when I call the super initialization on UIActionSheet
the variadic ...
at the end of the method aren't recognized as a va_list
and the action sheet only show the first button.
Here class implementation .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
And here how I initialize the action sheet:
[[[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];
And here the result:
I'm definitely doing something wrong but I do not understand what. Ideas ?
UIActionSheet
is not designed to be subclassed.
There are other ways how to use it with blocks. Best is to create a category, that implements delegate protocol and store the block using associated ojects mechanism. Implementation here.
I think initWithTitle:delegate:cancelButtonTitle:...
is not the designated initializer and it is implemented using [self init]
with following setTitle:
and addButtonWithTitle:
calls. You should do it similarily.
In fact, with variadic method, you have no other option. To collect all arguments, you have to use va_list
and related functions. Implementation here. Then on each of them you call addButtonWithTitle:
as I mentioned.