Skip to content

AA 1.12. Implementing Navigation with UINavigationController

rayastar edited this page Feb 7, 2015 · 1 revision

Let’s start with the .m file of our app delegate:

//  AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UINavigationController *navigationController;
@property (strong, nonatomic) UIWindow *window;
@end

Now we have to initialize our navigation controller using its initWithRootViewCon troller: method and pass our root view controller as its parameter. Then we will set the navigation controller as the root view controller of our window. Don’t get confused here. UINavigationController is actually a subclass of UIViewController, and our window’s rootViewController property accepts any object of type UIView Controller, so if we want the root view controller of our window to be a navigation controller, we simply set our navigation controller as the root view controller of the window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    FirstViewController *viewController = [[FirstViewController alloc]
                                           initWithNibName:nil
                                           bundle:nil];
    self.navigationController = [[UINavigationController alloc]
                                 initWithRootViewController:viewController];
    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.navigationController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;

Let’s go to our root view controller’s implementation file, inside the viewDidLoad meth‐ od, and set the title property of our view controller to First Controller. We’ll also create our button there. When the user presses this button, we want to display the second view controller on the screen:

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@property (nonatomic, strong) UIButton *displaySecondViewController;
@end

-(void) performDisplaySecondViewController:(id)paramSender{
    SecondViewController *secondController = [[SecondViewController alloc]

                                              initWithNibName:nil
                                              bundle:NULL];
    [self.navigationController pushViewController:secondController
                                         animated:YES];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    //1.12
    self.title = @"First Controller";
    self.displaySecondViewController = [UIButton
                                        buttonWithType:UIButtonTypeSystem];
    [self.displaySecondViewController
     setTitle:@"Display Second View Controller"
     forState:UIControlStateNormal];
    [self.displaySecondViewController sizeToFit];
    self.displaySecondViewController.center = self.view.center;
    [self.displaySecondViewController
     addTarget:self action:@selector(performDisplaySecondViewController:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.displaySecondViewController];
}

Now let’s go and create this second view controller, without a .xib file, and call it Second ViewController. Follow the same process that you learned in Recipe 1.9. Once you are done creating this view controller, give it a title of Second Controller:

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Second Controller";
}

Now what we want to do is “pop” from the second view controller back to the first view controller, five seconds after the second view controller is displayed to the screen. For that we are using the performSelector:withObject:afterDelay: method of NSOb ject to call our new method, goBack, five seconds after our second view controller successfully displays its view. In the goBack method, we are simply using the naviga tionController property of our view controller (this is built into UIViewController and is not something that we coded) to pop back to the instance of FirstViewControl ler, using the popViewControllerAnimated: method of our navigation controller that takes a Boolean as a parameter. If this Boolean value is set to YES, the transition back to the previous view controller will be animated, and if NO, it won’t be. When the second view controller is displayed on the screen, you will see something similar to that shown:]


Clone this wiki locally