如何调用完成块?

时间:2016-03-14 13:06:26

标签: objective-c swift

如何在ObjectiveC中调用基于Swift的完成块?

public var completionBlock: IMGLYEditorCompletionBlock?
public typealias IMGLYEditorCompletionBlock = (IMGLYEditorResult, UIImage?) -> Void

这就是我在Swift中调用它的方式,但从未在Objective C中工作。

func callEditorViewController() {
   var editorViewController = IMGLYMainEditorViewController()
   editorViewController.highResolutionImage = image
   editorViewController.initialFilterType = .None
   editorViewController.initialFilterIntensity = 0.5
   editorViewController.completionBlock = editorCompletionBlock
}

func editorCompletionBlock(result: IMGLYEditorResult, image: UIImage?) {
   ...
}

这是我的客观C代码,语法错误。

-(void) editorCompletionBlock:(IMGLYEditorResult*)result img:(UIImage*)image{

}

 IMGLYMainEditorViewController *editor = [[IMGLYMainEditorViewController alloc]init];
editor.highResolutionImage = image;

 editor.completionBlock = [self editorCompletionBlock:r img:image];

2 个答案:

答案 0 :(得分:1)

调用块很简单:

editor.completionBlock(result, image);

仍然觉得你喜欢设置它:

editor.completionBlock = ^(IMGLYEditorResult result, UIImage *image) {
        // do something meaningful here 
};

答案 1 :(得分:0)

首先在.h文件中创建新类型

typedef void (^TableViewCellConfigureBlock)(id cell, id item);

然后在viewDidLoad

中在.m文件中定义您的块
TableViewCellConfigureBlock configureCell = ^(id cell, id object) {
            [cell configureCellForObject:object];
        };

最后从某处调用

UITableViewCell *cell;
NSString *item = @"my test string";

self.configureCellBlock(cell, item);