NavigationController with .xib File

Sometimes we wants to use different views and navigation controller for better user usability. For this reason we should use .xib file on behalf optimal control. Here all we need to do pretty basic!

Firstly go to File > New > File afterwards under the ios tab we are going to choose “Cocoa Touch > Objective-C Class” click and next.  Class name : FirstViewController and Subclass of UIViewController and don’t forget “Also create XIB File” option check. That’s it! Time to write little code!

AppDelegate Files are :

Interface File

#import <UIKit/UIKit.h>
#import "FirstViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong,nonatomic) FirstViewController  *firstViewController;
@property (strong,nonatomic) UINavigationController *navController;

@end

Implementation File


#import "AppDelegate.h"

@implementation AppDelegate

@synthesize navController;
@synthesize firstViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    // Setting first view controller
    self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
    self.firstViewController.title = @"First View Controller";
    self.navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    [self.window setRootViewController:self.navController];
    [self.window makeKeyAndVisible];
    return YES;
}

You have to notice that I’ve set rootViewController to my firstViewController.
Also we have two UIViewController for to navigate each of them.

FirstViewController :

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface FirstViewController : UIViewController

@property (nonatomic,strong) SecondViewController *secondViewController;

-(IBAction) pushSecondView:(UIButton *)sender;

@end


#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize secondViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(SecondViewController *) secondViewController
{
    if(!secondViewController) {
        secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
        secondViewController.title = @"Second View Controller";
    }
    return secondViewController;
}

-(void)pushSecondView:(UIButton *)sender
{
    [self.navigationController pushViewController:self.secondViewController animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

SecondViewController

Interface

#import <UIKit/UIKit.h>
#import "ThirdViewController.h"

@interface SecondViewController : UIViewController

@property(nonatomic,strong) ThirdViewController *thirdViewController;

-(IBAction)gotoThirdScene:(UIButton *)sender;

@end


Implementation


#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize thirdViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark Lazly instantiate
-(ThirdViewController *) thirdViewController
{
    if(!thirdViewController) {
        thirdViewController = [[ThirdViewController alloc] initWithNibName:nil bundle:nil];
        thirdViewController.title = @"Third View Controller";
    }
    return thirdViewController;
}


-(void)gotoThirdScene:(UIButton *)sender
{
    [self.navigationController pushViewController:self.thirdViewController animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end