Adding a Navigation Controller to Your iOS App

Posted by kim in iOS

I’m still new to the iOS-game, and find many tutorials way too complicated. All I wanted was to add a second view to my app, and add that little neat back-button to get back to the first view again.

This is done by, instead of adding your view directly to your window, you add it to a navigation controller, and then add the navigation controller to the window instead.

All I had to do was to modify my AppDelegate.m a bit, like this:

(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor blackColor];

// Init first view controller
_randomViewController = [[RandomViewController alloc] init];

// Init the navigation controller with the first view controller as
root
UINavigationController *navController = [[UINavigationController
alloc] initWithRootViewController:_randomViewController];

// Set window's root view controller to the navigation controlller
self.window.rootViewController = navController;

[self.window makeKeyAndVisible];
return YES;

Leave a Comment

Your email address will never be published or shared. Required fields are marked with an asterisk (*).