Could not understand how SimpleDrillDown iPhone example project works correctly!

Discussion in 'Mac Programming' started by bengimizrahi, May 24, 2008.

  1. bengimizrahi macrumors newbie

    bengimizrahi

    Joined:
    May 24, 2008
    Location:
    Istanbul
    #1
    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];
    }
     
  2. Wes macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #2
    I have no Cocoa programming experience but is it possible that it extends or implements something that does have a navigationController?
     
  3. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    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.
     
  4. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #4
    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.
     
  5. bengimizrahi thread starter macrumors newbie

    bengimizrahi

    Joined:
    May 24, 2008
    Location:
    Istanbul
    #5
    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.
     

Share This Page