0

I was trying to modally present a UINavigationController with a UITableViewController as it's root view but kept crashing the app when pressing the button to present the modal view.

- (IBAction)flipToDefaultsViewController:(id)sender
{

    RootTableViewController *controller = [[RootTableViewController alloc] initWithNibName:@"RootTableViewController" bundle:nil];

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:controller];
    nc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentModalViewController:nc animated:YES];
}

The app crash with the message:

[RootTableViewController numberOfSectionsInTableView:]: message sent to deallocated instance 0x5677b5

When I loaded up Instruments to take a further look it was apparent that two instances of my UITableViewController were created, one with the owner of the UINavigationController and the other by UIKit. The UIKit created instance was the one that was deallocated and causing the crash.

When I changed the initialisation from initWithNibName:bundle: to init the UITableViewController loaded fine (my .xib file was the same name as the class).

My question is why would this happen?

Should you not initialise a UITableViewController this way when adding it to a UINavigationController? I've had a hunt around the documentation with no joy so far.

Using iOS 5 with ARC but target deployment is 4.0.

etolstoy
  • 1,798
  • 21
  • 33
craigmarch
  • 534
  • 3
  • 9

2 Answers2

1

I haven't worked out why the object was being initialised twice, however I did review the steps that I used to create the .xib file and it looks like there is a problem with copying a view from a Storyboard to Interface Builder. In hindsight this makes sense, but as the view appears to copy without error and everything else seems to look okay it's easily done.

It would appear that similar problems were experienced by others with similar results.

By creating a completely clean subclass of UITableViewController with a nib file (⌘-N) and copying code from the initial class into the new one I'm able to use the initial code above to alloc/init my modal view.

BTW I was mistaken in my opening post about the nib file loading correctly when using init. It wasn't and in fact this behaviour doesn't happen for UITableViewController apparently where as other classes having a class name the same as the .xib file will attempt to load the .xib first.

Community
  • 1
  • 1
craigmarch
  • 534
  • 3
  • 9
  • This exact same thing happened to me and was _very_ hard to track down. Your comment saved me many more hours of frustration! – Alec Thomas Mar 07 '12 at 01:07
  • In a project with ARC enabled, copying a view from a storyboard into a .xib caused the application to crash. Something in the copy was decrementing my view reference count as soon as I pushed the view. – Alec Thomas Mar 08 '12 at 01:37
0

If this is a button, than you should not initialize anything when the button is pressed. You should initialize beforehand and simply present the modalViewController when the button is pressed.

Is there any reason why the rootViewController and the navigation controller cannot be initialized in the appdelegate didFinishLaunchingWithOptions method?

etolstoy
  • 1,798
  • 21
  • 33
Zigglzworth
  • 6,645
  • 9
  • 68
  • 107
  • But surely you don't want to initialise an object way before it's being used and with a possibility that it might never be used??? – craigmarch Jan 23 '12 at 15:17