【问题标题】:How to properly subclassing a UIActionSheet如何正确子类化 UIActionSheet
【发布时间】:2014-03-04 09:31:33
【问题描述】:

我希望子类 UIActionSheet 使用块方法而不是委托。 我的问题是当我在UIActionSheet 上调用超级初始化时,方法末尾的可变参数... 不会被识别为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];

结果如下:

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

【问题讨论】:

标签: ios objective-c ipad uiactionsheet


【解决方案1】:
  1. UIActionSheet设计为子类化

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

  3. 我认为initWithTitle:delegate:cancelButtonTitle:... 不是 指定 初始化程序,它是使用[self init] 实现的,并带有以下setTitle:addButtonWithTitle: 调用。你也应该这样做。

  4. 事实上,使用可变参数方法,您别无选择。要收集所有参数,您必须使用va_list 和相关函数。 Implementation here. 然后像我提到的那样,在他们每个人上调用addButtonWithTitle:

【讨论】:

    【解决方案2】:

    请在otherButtonTitles 参数的最后传递nil

    [[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];
    

    【讨论】:

    • - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /**/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
    猜你喜欢
    • 2016-12-31
    • 2022-01-25
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2011-08-10
    • 2016-04-19
    相关资源
    最近更新 更多