views:

868

answers:

2

I have a UITableView and I have set up didSelectRowAtIndexPath, which gets called (I have verified it using NSLog) every time user clicks on a row.

However, I want to reload the same view and change a different data. For example, I am displaying content of a directory on a remote web server, and as soon as user clicks on the row, I want to reload the view and display the contents of the selected directory (row).

I am using this code:

FirstViewController *fvController = [[FirstViewController alloc] initWithNibName:@"MainWindow" bundle:[NSBundle mainBundle]];
 fvController.currentDirectory = currentDirectory;
 [self.navigationController pushViewController:fvController animated:YES];
 [fvController release];
 fvController = nil;

However, it doesn't do anything and program keeps running. I assume I am missing a connection in the IB?

+1  A: 

The reason nothing happens is likely that self.navigationController is nil, probably because you never set one up. Your instincts are good: when "nothing happens" it almost always means that something is nil, and the #1 reason for something being nil is that you didn't wire it in IB.

Your recursive design shows a good understanding of how view controllers work. Looks like you're on the right path. Some unsolicited advice to move you further along. Instead of this code:

FirstViewController *fvController = [[FirstViewController alloc] initWithNibName:@"MainWindow" bundle:[NSBundle mainBundle]];
fvController.currentDirectory = currentDirectory;

I recommend something that looks like this:

FirstViewController *fvController = [[FirstViewController alloc] initWithDirectoryPath:currentDirectory];

You would need to write -initWithDirectoryPath: to call -initWithNibName:bundle:, of course. This pushes the name of the NIB inside the view controller that manages it, allows you to make -currentDirectory readonly or private, so no one messes with it behind the view controller's back, and generally makes the code easier to understand and maintain.

Rob Napier
Rob, thank you very much for your comments and the additional advice.I checked self.navigationController and it is not nil (it is type of UINavigationController) and it has been wired up correctly in the IB as far as I know. Still, when I click on the row, nothing happens. Any other debug tips?
In a nutshell, how do I check to see if pushViewController is called?
Have you set a breakpoint at the pushViewController line to make sure it is running? If the target is not nil, and the line runs, then pushViewController is called. The second most likely issue (after the navcontroller being nil) is that the entire block of code is not running.
Rob Napier
A: 

Thanks for your suggestions Rob. It turned out I had to BUILD CLEAN (from the build menu) and then build the project. For some reason, XCODE hadn't picked up the connection between my tableView and the UI element and its value was NIL.