如何在Swift中调用Objective-C块属性

时间:2017-02-11 05:10:27

标签: ios objective-c swift objective-c-blocks

我正在重写 Swift 中的一些 Objective-C 代码。它是一个名为ActionSheetPicker的开放库的子类。 所以这是我的 Objective-C 代码:

#import "XXActionSheetStringPicker.h"

@implementation XXActionSheetStringPicker
+ (instancetype)pickerWithTitle:(NSString *)title
                           rows:(NSArray *)strings
               initialSelection:(NSInteger)index
                      doneBlock:(ActionStringDoneBlock)doneBlock
                    cancelBlock:(ActionStringCancelBlock)cancelBlockOrNil
                         origin:(id)origin
{
    XXActionSheetStringPicker *picker = [[self alloc] initWithTitle:title
                                                               rows:strings
                                                   initialSelection:index
                                                          doneBlock:doneBlock
                                                        cancelBlock:cancelBlockOrNil
                                                             origin:origin];

    //do something specific stuff

    UIBarButtonItem *cancelBarButton = [self itemWithtitle:NSLocalizedString(@"GLOBAL_CANCEL", nil) action:@selector(cancelAction) target:picker];
    [picker setCancelButton:cancelBarButton];

    UIBarButtonItem *doneBarButton = [self itemWithtitle:NSLocalizedString(@"GLOBAL_OK", nil) action:@selector(doneAction) target:picker];
    [picker setDoneButton:doneBarButton];

    return picker;
}

+ (UIBarButtonItem *)itemWithtitle:(NSString *)title
                            action:(SEL)action
                            target:(id)target
{
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //do something specific stuff
    [cancelButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    return cancelBarButton;
}

- (void)cancelAction {
    [self onActionSheetCancel];
}

- (void)doneAction {
    [self onActionSheetDone];
}

@end

这就是我在Swift中尝试做同样的事情:

import ActionSheetPicker_3_0


class XXActionSheetStringPicker: ActionSheetStringPicker {


    override init!(title: String!,
                   rows strings: [Any]!,
                   initialSelection index: Int,
                   doneBlock: ActionSheetPicker_3_0.ActionStringDoneBlock!,
                   cancel cancelBlockOrNil: ActionSheetPicker_3_0.ActionStringCancelBlock!,
                   origin: Any!) {

        super.init(title: title,
                   rows: strings,
                   initialSelection: index,
                   doneBlock: doneBlock,
                   cancel: cancelBlockOrNil,
                   origin: origin)

        //do somet specific stuff
        let cancelBarButton           = self.itemWithtitle(NSLocalizedString("GLOBAL_CANCEL", comment: ""), action: #selector(self.cancelAction), target: self)
        self.setCancelButton(cancelBarButton)

        let doneBarButton             = self.itemWithtitle(NSLocalizedString("GLOBAL_OK", comment: ""), action: #selector(self.doneAction), target: self)
        self.setDoneButton(doneBarButton)
    }

    func itemWithtitle(_ title: String,
                       action: Selector,
                       target: Any) -> UIBarButtonItem {

        let cancelButton = UIButton(type: .system)
        //do somet specific stuff
        cancelButton.addTarget(target, action: action, for: .touchUpInside)
        let cancelBarButton = UIBarButtonItem(customView: cancelButton)
        return cancelBarButton
    }

    func cancelAction() {
        self.onActionSheetCancel()
    }

    func doneAction() {
        self.onActionSheetDone()
    }
}

但我不能self.onActionSheetCancel(),因为'不能称之为非功能类型ActionStringCancelBlock的价值! (DoneBlock)错误。

块是:

typedef void(^ActionStringDoneBlock)(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue);
typedef void(^ActionStringCancelBlock)(ActionSheetStringPicker *picker);
@property (nonatomic, copy) ActionStringDoneBlock onActionSheetDone;
@property (nonatomic, copy) ActionStringCancelBlock onActionSheetCancel;

是否有任何修复/解决方法?感谢帮助。

0 个答案:

没有答案