如何在IOS中将回调方法作为参数传递

时间:2014-11-06 11:51:41

标签: ios objective-c iphone cocoa

我将根据以下示例解释需求

这是在异步操作

之后需要调用的方法
    -(void) myCallbackMethodOne: (NSString *)response
    {
        //method to be called
    }

    -(void) myCallbackMethodTwo: (NSString *)response
    {
        //method to be called
    }



    -(void) getDataFromServerWithCallback: (NSString *)requestString _Callback(CallbackMethod *) methodName
    {
        //logic to send request and 
        //to set callback method something similar to
        [setCallbackMethod  methodName]; 
    }

    -(void) onDataRecievedFromServerWithResponse: (NSString *) response //this method gets called as part of framework
    {
        [callTheCallbackMethod: response]; 
    }

调用方法来演示要求的地方

    -int main()
    {
        [getDataFromFromServerWithCallback: @"getTopNews" _Callback:myCallbackMethodOne];  //this is the requirement; I should be able to pass myCallbackMethod as argument

        [getDataFromFromServerWithCallback: @"getBusinessNews" _Callback:myCallbackMethodTwo];  //this is the requirement; I should be able to pass myCallbackMethod as argument

    }

3 个答案:

答案 0 :(得分:1)

这种功能有两种完善的模式:

1)代表:

@protocol ResponseDelegate
- (void)handleResponse:(NSString *)response;
@end

@interface CommsClass : NSObject
@property (weak) id<ResponseDelegate> delegate;
- (void)sendRequest:(NSString *)request;
@end

@interface CallingClass : NSObject <ResponseDelegate>
{
    CommsClass _commsClass;
}

- (void)callingCode;
@end

@interface CallingCode

- (void)callingCode
{
    _commsClass = [CommsClass new];
    _commsClass.delegate = self;
    [_commsClass sendRequest:@"Blah"];
}

- (void)handleResponse:(NSString *)response
{
    NSLog(@"Whoot: %@", response);
}

@end

2)阻止。

typedef (^HandleResponseBlock)(NSString *response);

@interface CommsClass : NSObject
- (void)sendRequest:(NSString *)request
withCompletionBlock:(HandleResponseBlock)block;
@end

答案 1 :(得分:0)

对于目标C,我相信你必须使用block传递回调。但对于Swift来说,由于方法也是一等公民,我们可以这样做:

func buttonDidTapped(sender: AnyObject!) {
    doSomethingWithCallback(callbackFunc: myCallback)
}

func doSomethingWithCallback(callbackFunc: (NSDictionary)->()) {
    //do something
    callbackFunc(["param": "pass any param by dynamic dictionary"])
}


func myCallback(infoDict: NSDictionary) {
    //callback implementation
}

我们可以定义回调和任何函数,并像任何参数一样传递它。

有关在同一项目中使用objective-c和swift的更多信息,请参阅Apple文档: https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html

希望它有所帮助。

答案 2 :(得分:0)

我通过使用选择器

来满足我的要求

- (void)myCallbackMethodOne:(NSString *)响应

{

    //method to be called

}


-(void) myCallbackMethodTwo: (NSString *)response
{
    //method to be called
}



-(void) getDataFromServerWithCallback: (NSString *)requestString _Callback:(SEL) methodName _Caller(id):callingClass
{
    //write the logic here to store methodname and caller to reference variables so that it will be accessible in onDataRecievedFromServerWithResponse
    //and to send the request

}

-(void) onDataRecievedFromServerWithResponse: (NSString *) response //this method gets called as part of framework
{
    [callingClass performSelector:methodName withObject:response];
}


 -int main()
{

    SEL methodOneSelctor =@selector(myCallbackMethodOne:);
    [getDataFromFromServerWithCallback: @"getTopNews" _Callback:methodOneSelctor _MethodCaller:self];  //this is the requirement; I should be able to pass myCallbackMethod as argument

    SEL methodTwoSelctor =@selector(myCallbackMethodTwo:);
   [getDataFromFromServerWithCallback: @"getBusinessNews" _Callback:methodTwoSelctor _MethodCaller:self];  //this is the requirement; I should be able to pass myCallbackMethod as argument

}