ThisDarkTao
ThisDarkTao

Reputation: 1062

Navigation controller push nil view controller error

I'm having some problems with a Navigation controller in my app. Moving from the RootViewController to a UITableViewController works fine. I now want to have one more level of drilldown, so users pick an item from the list and a new screen appears, like this:

RootViewController --> TableViewController --> ItemViewController

I've used the exact same code that switches the first views, but I get:

Application tried to push a nil view controller on target <UINavigationController...

The code is identical to the first one, so how can it be nil?

In RootViewController.h:

@interface RootViewController : UIViewController {
    IBOutlet TableViewController *tableViewController;
}

@property (nonatomic, retain) TableViewController * tableViewController;

In the .m file I synthesize the property and then use a button to call:

[self.navigationController pushViewController:tableViewController animated:YES];

In TableViewController.h:

#import "ItemDetailViewController.h"

@class TableViewController;

@interface TableViewController : UITableViewController {
    IBOutlet ItemDetailViewController * itemDetailViewController;
}

@property (nonatomic, retain) ItemDetailViewController * itemDetailViewController;

@end

And again, I synthesize it in the .m file and use code to push the new view in didSelectRowAtIndexPath:

[self.navigationController pushViewController:itemDetailViewController animated:YES];

When you tap on an item I get the error message above. Does anyone have any idea why this is happening?

Upvotes: 1

Views: 1862

Answers (1)

Rob Napier
Rob Napier

Reputation: 299265

This most common cause of this error is failing to connect itemDetailViewController to the actual object in IB. Note that you should generally place the IBOutlet designator on the property rather than on the ivar.

Upvotes: 1

Related Questions