views:

33

answers:

1

Hi all,

Long time lurker, first time poster.

I'm making a ServerConnection module to make it a whole lot modular and easier but am having trouble getting the delegate called. I've seen a few more questions like this but none of the answers fixed my problem.

ServerConnection is set up as a protocol. So a ServerConnection object is created in Login.m which makes the call to the server and then add delegate methods in Login to handle if there's an error or if it's done, these are called by ServerConnection like below.

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

    if( [self.delegate respondsToSelector:@selector(connectionDidFinish:)]) {
        NSLog(@"DOES RESPOND");
        [self.delegate connectionDidFinish:self];
    } else {
        NSLog(@"DOES NOT RESPOND");
    }

    self.connection = nil;
    self.receivedData = nil; 

}

It always "does not respond". I've tried the CFRunLoop trick (below) but it still doesn't work.

- (IBAction)processLogin:(id)sender {

    // Hide the keyboard
    [sender resignFirstResponder];

    // Start new thread
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Acutally call the server
    [self authenticate];

    // Prevent the thread from exploding before we've got the data
    CFRunLoopRun();

    // End thread
    [pool release];

}

I copied the Apple URLCache demo pretty heavily and have compared them both many times but can't find any discrepancies.

Any help would be greatly appreciated.

+2  A: 

Here are the questions to ask:

  • Does your delegate respond to connectionDidFinishLoading:?
  • Does the signature match, i.e. it takes another object?
  • Is the delegate set at all or is it nil? (Check this in that very method)

If any of those are "NO", you will see "doesn't respond"... and all equally likely to happen in your application, but all are easy to figure out.

Eiko
Many thanks for that. It was missing the `self.delegate = theDelegate;` in this function `- (id) initWithURL:(NSString *)theURL withData:(NSString *)theData delegate:(id<ServerConnectionDelegate>)theDelegate; {`