Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bengimizrahi

macrumors newbie
Original poster
May 24, 2008
13
0
Istanbul
In the last line of this method, howcome [self navigationController] does not give runtime error? self is actually RootViewController and it does not have a property named navigationController? What am I missing here?

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	// Create the detail view lazily
	if (detailViewController == nil) {
		DetailViewController *aDetailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
		self.detailViewController = aDetailViewController;
		[aDetailViewController release];
	}
	// Set the detail controller's inspected item to the currently-selected item
	SimpleDrillDownAppDelegate *appController = (SimpleDrillDownAppDelegate *)[[UIApplication sharedApplication] delegate];
	detailViewController.detailItem = [appController objectInListAtIndex:indexPath.row];
	[[self navigationController] pushViewController:detailViewController animated:YES];
}
 

4409723

Suspended
Jun 22, 2001
2,221
0
I have no Cocoa programming experience but is it possible that it extends or implements something that does have a navigationController?
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
There is some Cocoa "voodoo" going on there. The code gets an instance of the application delegate object just before the line you are looking at:

Code:
	SimpleDrillDownAppDelegate *appController = (SimpleDrillDownAppDelegate *)[[UIApplication sharedApplication] delegate];

It's not obvious, but that is where the navigationController property is accessed from.

Kind of a bad programming style to use that coding shortcut in a sample code project, but it happens everywhere.
 

lucasgladding

macrumors 6502
Feb 16, 2007
319
1
Waterloo, Ontario
There is some Cocoa "voodoo" going on there. The code gets an instance of the application delegate object just before the line you are looking at:

Code:
	SimpleDrillDownAppDelegate *appController = (SimpleDrillDownAppDelegate *)[[UIApplication sharedApplication] delegate];

It's not obvious, but that is where the navigationController property is accessed from.

Kind of a bad programming style to use that coding shortcut in a sample code project, but it happens everywhere.

Actually that's not the case. The application delegate is simply used to get the model object the detailViewController will be editing.

The RootViewController is a subclass of UIViewController and inherits the navigationController property from that. I imagine that when any view controller is pushed onto the navigationController, the navigationController property is set.

@bengimizrahi:

Inheritance is one of the first things you need to understand when working with Cocoa. The Objective-C documentation is great once you get your head around it. The class documentation gives you this inheritance information at the beginning of each class. I always make a habit of going through this info when working with classes for the first time.
 

bengimizrahi

macrumors newbie
Original poster
May 24, 2008
13
0
Istanbul
Oh, I see :)

And thank you for the advice :) I just checked out the first superclass which is UITableViewController and forgot to look at the others in the hierarchy.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.