I’m having a problem with the following code. As you can a TripDetailsController is created (subclass of UIViewController) and this one is initialized with the initWithNibName method. This initWithNibName takes an extra argument (tripDetails), see code of TripDetailsController class below. The variable td has a value and when stepping through the code, I checked that this value was set on the tdController when the initWithNibName method is executed. However, when setting a breakpoint in the TripDetailsController.viewDidLoad method (which is executed when pushViewController() is called) the tripDetails attribute of the TripDetailsController is nil ?!?
Using the debugger I discovered that the tdController instance that I see in the initWithNibName method is a different instance than the instance that I see in the viewDidLoad method? How come? Why is there an new instance of the TripDetailsController created and by who?
At the end of this post I included a dump of call stack, maybe that give you a hint for the cause of this problem.
Code:
// Code from RootViewController.getTripDetails() (see stacktrace below)
TripDetails td = ...; // td has a value
// Push the detail view controller
TripDetailsController *tdController = [[TripDetailsController alloc] initWithNibName:@"TripDetailsController" bundle:nil tripDetails:td];
// Checked using the debugger that tdController.tripDetails is set
[self.navigationController pushViewController:tdController animated:YES];
[tdController release];
Code:
@implementation TripDetailsController
@synthesize tripDetails, expectedDurationLabel, normalDurationLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Reis Details";
expectedDurationLabel.text = [NSString stringWithFormat:@"%@", tripDetails.expectedDuration];
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle tripDetails:(TripDetails *)td {
self = [self initWithNibName:nibName bundle:nibBundle];
self.tripDetails = td; // self.tripDetails has a value here!
[self.tripDetails retain];
return self;
}
- (void)dealloc {
[self.tripDetails release];
[super dealloc];
}
@end
alt text http://gerodt.homeip.net/stack.png
Any suggestions are welcome.
Gero
I found a way to get it working by following the sample code from the SimpleDrillDown example from Apple.
Main differences with my original code:
Code:
As a result I had to tweak the layout a bit, but at least it now works.
Looks as if it is not possible to initialize a view controller from a XIB file and then push it on to the navigation controller.
Gero