Can You Tell Me Why This Works

Discussion in 'iOS Programming' started by zachsilvey, Dec 18, 2010.

  1. zachsilvey macrumors 6502

    Joined:
    Feb 5, 2008
    Location:
    Battle Ground
    #1
    In *AppDelegate.h I create a UINavigation Controller and make it a property

    Code:
    UINavigationController *navigationController;
    @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
    then in *AppDelegate.m I use

    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        
    	RootViewController *rootViewController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
    	navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
    
    	[self.window addSubview:navigationController.view];
    	
    	[rootViewController release];
    	
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    
    Why is it in the RootViewController.m I can call this?

    Code:
    [self.navigationController pushViewController:secondLevelViewController animated:YES];
    navigationController doesn't belong to that class so why is it that I can call "self.navigationController"?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Because all view controllers have a navigationController property (it's in the documentation). When you add a view controller to a navigation controller (either by pushing it or initing the navigation controller with a root view controller) it get's set by the navigation controller.
     
  3. zachsilvey thread starter macrumors 6502

    Joined:
    Feb 5, 2008
    Location:
    Battle Ground
    #3
    So it was just by chance that I named the UINavigationController "navigationController'?

    I should spend more time in the documentation.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes. That had absolutely no impact on it.
     

Share This Page