Saturday, June 8, 2013

UINavigationController topViewController example in Objective C (iOS).


UINavigationController topViewController

The view controller at the top of the navigation stack. (read-only)

@property(nonatomic, readonly, retain) UIViewController *topViewController

UINavigationController topViewController example.
- (void)applicationDidFinishLaunching:(UIApplication *)application {

    RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
    rootViewController.managedObjectContext = self.managedObjectContext;

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

Example of [UINavigationController topViewController].
Try topViewController instead of visibleViewController.

FirstViewController *fVC = [navController topViewController];

UINavigationController topViewController example.
Implement the UINavigationControllerDelegate method:

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
and query it to find out the currently displayed view controller:

 navigationController.topViewController
This is the one you're coming from.


End of UINavigationController topViewController example article.