Question

I am a relatively new iPhone app developer so my knowledge is a little sketchy, so please forgive me if this is a bit of a trivial question.

I have a navigation app which drills between table views by calling pushViewController on the navigationController object

I have one particular section which pushes new view controllers sequentially as the user goes through the interface. The view controllers are all subclassed from UIViewController.

It all works fine until it gets to the end, where the idea is the user presses a "Finish" button and is returned to the root view controller (main menu).

So on the button press I call:

[[self navigationController] popToRootViewControllerAnimated:YES];

And it crashes.

I am a bit worried this could be a big problem as this definitly worked at some point but it is now always failing.

Can anyone give any ideas/advice?

Was it helpful?

Solution

Some suggestions:

  • Before calling popToRootViewControllerAnimated: confirm that the RootViewController does actually exist. If it died somewhere along the line, calling the method will cause a crash.
  • Check the – viewWillDisappear: and – viewDidDisappear: methods of your last view to make sure you're not doing something dangerous there.
  • Not sure if popping a view causes it to always deallocate but check the dealloc method of the views and their controllers to make sure your not over-releasing something.
  • One mistake I've seen a lot is releasing objects in the data model from controllers. When another controller (in this case the RootViewController) tries to access the data model the app crashes.

It sound's like you need how to use the Xcode debugger. Type in debugger in Xcode help to get pointers.

OTHER TIPS

You should not be using popToRootViewController in your viewWillDisappear. Instead if you want to pop to root controller on your pressing the back button, you should replace the back button by your own and add an action to it. Try doing something like ::

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"back"
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(goBack:)]; 
self.navigationItem.leftBarButtonItem = back;

and then handle the action as ::

- (void) goBack:(id)sender
{
    // pop to root view controller
    [self.navigationController popToRootViewControllerAnimated:YES];

}

As the others have commented, the first step is to run this is debug mode and figure out where and why you are crashing.

The most common type of crash is using a deallocated object (EXEC_BAD_ACCESS). Have you run the static analyzer? Are you properly retaining your object references?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top