2
votes

I have a view in my app that displays a UITableView. This view is created in a nib file and has a custom view controller. The UIViewController subclass for this view acts as the Datasource and Delegate for the UITableView.

My UITableView displays several rows based on my data. Then, the last row displays different text: "Add another...". If the last row is selected, I present a modal view controller (to allow the user to add more data). When I dismiss the modal view controller, I again see the original view (as expected) and all appears to be well. However, when I try to interact with this view, the app crashes.

From placing several NSLog() statements through the UIViewController (for the UITableView), I have determined that the -dealloc method is being called just after the modal view is dismissed. This explains the crash when I try to do something with that view. However, I have no idea why -dealloc is being called on this view controller.

To dismiss the modal view controller, I have:

[self dismissModalViewController:YES];

As the code in an IBAction method in the modal view controller's UIViewController. This action is tied to a cancel button in the corresponding nib file.

In addition, my understanding from the View Controller Programming Guide is that it's OK to dismiss the modal controller from within itself, but it's more robust to use delegates. I was initially using a delegate, but took the delegate out to simplify debugging. I just put the delegate back in to double-check, and the same behavior occurs when using delegates. The modal controller's action method calls is implemented as:

[[self delegate] myModalViewController:self didAddObject:obj];  

The delegate implementation in the parent view controller is:

[self dismissModalViewController:YES]

If anyone has seen this before or has any suggestions of what could be happening or how to debug this, I would greatly appreciate it.

2
You mean the parent view controller is the one being deallocated?BoltClock

2 Answers

6
votes

If -dealloc is being called, something is releasing the view controller. Try implementing -release in your view controller:

-(void)release {
    NSLog(@"view controller released");
    [super release];
}

so that you can use the debugger to inspect the call stack when this unexpected release message happens.

3
votes

Its dangerous to call dismissModalViewController from the modal view controller itself (message will be forwarded to parent view controller), if you have not retained it elsewhere. Normally, the parent view controller is responsible for dismissing the modal view controller it presented.