如何正确地继承UIActionSheet

时间:2014-03-04 09:31:33

标签: ios objective-c ipad uiactionsheet

我希望子类a UIActionSheet使用块方法而不是委托。 我的问题是当我在UIActionSheet上调用超级初始化时,方法末尾的可变参数...不会被识别为va_list而操作表只显示第一个按钮。< / p>

此类实现.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];

结果如下:

enter image description here

我肯定做错了什么,但我不明白。 想法?

2 个答案:

答案 0 :(得分:3)

  1. UIActionSheet 设计为子类

  2. 其他方式如何将其与块一起使用。最好是创建类别,它实现委托协议并使用关联的对象机制存储块。 Implementation here.

  3. 我认为initWithTitle:delegate:cancelButtonTitle:... 指定的初始值设定项,并使用[self init]以及setTitle:来实现addButtonWithTitle:来电。你应该类似地做。

  4. 事实上,使用可变方法,您没有其他选择。要收集所有参数,您必须使用va_list和相关功能。 Implementation here.然后,就我提到的每个人都致电addButtonWithTitle:

答案 1 :(得分:0)

请在nil参数的最后一位传递otherButtonTitles

[[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", nil]
     showFromBarButtonItem:myBarButtonItem
     animated:YES];