views:

82

answers:

2

I have a function, in UpdateViewController, that is being called by a delegate, MyDownloadController, that will close a modal view (which is the UpdateViewController).

-(void)errorDownloading {
    self.downloadController.delegate = nil;
    [downloadController release];

    [self dismissModalViewControllerAnimated:YES];
} 

I've tried doing this with and without messing with the delegate pointer and it still doesn't close the view.

The delegate calls the method like this within MyDownloadController:

-(void)connectionError {
    if([delegate respondsToSelector:@selector(errorDownloading)]){
        [delegate errorDownloading];
    }
}

And this function is called by a different delegate (MyConnectionController).

Is there anything wrong with having this many delegates? And would a pointer error or something with them effect the modalview being able to close? If so, how / why?

I have this structure for the delegations:

UpdateViewController (the actual modal view I am trying to close)
|- MyDownloadController (the controller that abstracts the process being done)
    |- MyConnectionController (a helper class I wrote to interact with NSURLConnection)
        |- NSURLConnection

What is the best way to diagnose this problem?

+2  A: 

If downloadController is the view you want dismissed, I believe you're releasing it too soon.

-(void)errorDownloading {
    [self dismissModalViewControllerAnimated:YES];

    self.downloadController.delegate = nil;
    [downloadController release];
} 
BarrettJ
Sorry, I should have named the controllers as to be more clear on what was happening. I will update the question to clarify this.
RyanJM
A: 

Apple documentation says:

dismissModalViewControllerAnimated:

Dismisses the modal view controller that was presented by the receiver.

Mean's you call the the dismissModalViewControllerAnimated: method on the viewController that presented the ModalViewController you want to dismiss. in your case, this is the correct code to use.

-(void)errorDownloading {
    self.downloadController.delegate = nil;
    [downloadController release];

    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

Also answer to your other questions on number of delegates and pointers. Better design usually means you don't have huge strings of delegate objects but there's little reason to say thats wrong, it just gets messy IMHO. Pointers and the such as you described would most likely cause leaks or crashes, the reason it won't close is what I specified above, you weren't calling the method to the proper receiver.

gauravk92
If you are trying to abstract various parts what is the better way to do it other than delegates?And using self.parentViewController isn't working. Any other ideas?
RyanJM