Reputation: 10039
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.
Upvotes: 3
Views: 260
Reputation: 8423
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 {
...
}
}
Upvotes: 1
Reputation: 8905
MyHttpCallbacks *callbacks = [[MyHttpCallbacks alloc] init];
request.delegate = callbacks;
Upvotes: 4
Reputation: 964
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.
Upvotes: 0