In the previous tutorial Using UINavigationController component into your iOS App Part One we saw how easy is to develop application with multiple UIViewController-s using Storyboard and UINavigationController as navigation mechanism. With only few click we can connect all relations and easy we can implement simple fully functional prototype for presentation which can be exported, distributed and installed on device.
In this part, part two we will see other approach of using UINavigationController but this time with manually initialization in the application AppDelegate and navigating between view controllers manually pushing view controllers to navigation stack. Some of the developers prefer previous approach and some this approach, sometimes its matter of choice and sometimes its depends on the project.
We are starting with creating the Single View Application in our Xcode IDE.
Next we are setting Product Name, Organisation Name, Identifier, Language ( Objective-C or Swift ) and Device.
Your project should look like this after finishing previous step:
Because in this tutorial we will not use views and view controllers from the storyboard first we need to create one view for our existing UIViewController.
We are selecting from the menu iOS / User Interface and then we are selecting View icon.
For our new created view we are setting the size and we are choosing compact, portrait for all iPhones.
Next we are selecting File’s Owner and in Identity Inspector for Class we are selecting ViewController, the class where the whole logic connected with this view will be written.
From Object Library we are dropping to the view two buttons same like in the previous example from tutorial Using UINavigationController component into your iOS App Part One and we are making small customization on default buttons with setting background colors etc.
For buttons we are creating action methods by dragging from the Interface Builder over Assistant Editor.
We must associate View from Interface Builder with View Outlet in Connection Inspector otherwise the view from the xib file will not be connected with view from view controller and the application will crash with exception.
In applications AppDelegate first we are adding class property for UINavigationController, then in the first method, didFinishLaunchWithOptions, we are making instance from our first view controller. After that we are making initialization of class property of uiNavigationController and as rootViewController we are setting the instance of our first view controller so this will be the first view what we will see after starting the application. When we finish we initialization next we are initializing window class property and for rootViewController we are setting the instance of UINavigationController, uiNavigationController.
For our example we must create two more UIViewController-s so in the menu we are selecting New File.
We are choosing type of source file, Cocoa Touch Class.
On the next window we are checking the checkbox, Also create XIB file.
After creating the second view controller we are continuing with creating third view controller. When we finish our project should look like on the image bellow.
For new created xib files first we must select size of the view to match for all iPhones and then we are changing the background color to match the color on the buttons. Its good to mention that for new created view controllers that come with created xib files we dont need to set File’s Owner Class name and to make connection from the view to the view from the controller because when we created this view controllers with corresponding xib files they were automatically connected.
In first view controller class in method viewDidLoad we are setting manually the property title an that we are doing the same for the second and third view controller.
In action methods we are making instance from new created view controllers we corresponding xib file names and then we are calling the method which came from the UINavigationController, .pushViewController where we pass the instance of the previously instantiated view controllers.
When we compile and run the application in the simulator we can press buttons so we can see if everything we have implemented correctly how its working.
Complete source code can be found on my GitHub ready for testing.