问题
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