3

I am an iOS newbie. I want to specify a success callback for an Http execution. However, that is in another class. How would I specify it? I tried the following -

[request setDidFinishSelector: @selector([[MyHttpCallbacks get] successHttpMethod]:)];

The callback functions are defined in the MyHttpCallbacks. This does not work. If I define the methods in the same class and use it like this it works fine -

[request setDidFinishSelector: @selector(successHttpMethod:)];

Any help would be appreciated.

3 Answers 3

4
MyHttpCallbacks *callbacks = [[MyHttpCallbacks alloc] init];
request.delegate = callbacks;
Sign up to request clarification or add additional context in comments.

1 Comment

It is indeed as simple as that.
1

I had the same problem and I made the HTTP handling class (ServerComm in my case) to call a delegate method.

ServerComm.h I added at the end

@interface NSObject (ServerCommDelegate)
- (void)finishedLoading:(NSString*)result success:(BOOL)success;
@end

ServerComm.m

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    ...

    if ( [delegate respondsToSelector:@selector(finishedLoading:success:)] ) {
        [delegate finishedLoading:aStr success:YES];
    }    
}

In the main class after creating the instance

serverCommObj.delegate = self;

and then the method

- (void)finishedLoading:(NSString*)result success:(BOOL)success 
{
    if (!success) {
        ...
    } else {
        ...
    }    
}

Comments

0

I believe natively what you want to achieve is not possible (unless someone have a hack and I am unaware). I had this problem many times that I had to change my design and include the callback in the same class as where it is created.

Although, come to think of it. (This is a suggestion that I didn't try myself) You could have your callback class "include" in the main and then from there, call a selector function that calls the function inside the callback. Not a very efficient hack, but i think it will work.

So something like this:

[request setDidFinishSelector: @selector(callbackFunction)];

With the call back in the same class:

- (void)callbackFunction {
    [MyHttpCallbacks get] successHttpMethod];
}

Where successHttpMethod reside in another class and you include it as an import.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.