How to invoke Objective-C block property in Swift

▼魔方 西西 提交于 2019-12-11 08:48:34

问题


I am rewriting some Objective-C code in Swift. It is a subclass from an open library called ActionSheetPicker. So here is my Objective-C code:

#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

And this is how I try do the same in 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()
    }
}

But I can't do self.onActionSheetCancel(), coz of ' Cannot call value of non-function type ActionStringCancelBlock! (DoneBlock) error.

Blocks are:

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

Is there any fix / workaround for this? Thanks for help.

来源:https://stackoverflow.com/questions/42172472/how-to-invoke-objective-c-block-property-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!