ios - 如何正确子类化 UIActionSheet

标签 ios objective-c ipad uiactionsheet

我想要一个子类 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];

结果如下:

enter image description here

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

最佳答案

  • UIActionSheet不是 设计为 子类 .
  • 其他方式如何将它与 block 一起使用。最好是创建一个类别 ,实现委托(delegate)协议(protocol)并使用 存储 block 相关项目 机制。 Implementation here.
  • 我认为 initWithTitle:delegate:cancelButtonTitle:...不是 指定初始化器,它是使用 [self init] 实现的有以下 setTitle:addButtonWithTitle:来电。你应该这样做。
  • 事实上,使用可变参数方法,您别无选择。至收集所有论据 , 你必须使用 va_list及相关功能。 Implementation here.然后在他们每个人上调用addButtonWithTitle:如我所说。
  • 关于ios - 如何正确子类化 UIActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167890/

    相关文章:

    ios - 创建配置文件以与 TestFlight 一起使用?

    iphone - 将 ViewController 分配给委托(delegate),好不好?

    css - 类似今日美国的 iPad 多页分栏布局

    ipad - 无法安装应用程序 iPad,但它可以在 Xamarin.Forms 的 iPhone 中运行

    iphone - 预期标识符?什么?

    ios - 加载 txt 文件 Swift

    iphone - iOS 6 应用程序 - 如何处理 iPhone 5 的屏幕尺寸?

    ios - 更新 (15) 后现有部分中包含的行数必须等于

    ios - SWIFT - xcode 6 beta 6 中的 KVO .. observeValueForKeyPath 不再被调用

    iphone - iPad 中的大对话框