1

How do you add a navigation controller on a newly created view controller? i've search everywhere but all the tutorials are from creating a navigation controller project.

Anyone can lead mo to a tutorial that creates a navigation controller using a view controller subclass?

What i'm doing so far:

I created a UIViewController Project, and i have something like this to go to another view controller, with a navigation controller.

NavController *view=[[NavController alloc] initWithNibName:nil bundle:nil];
view.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:view animated:YES];
[view release];

Added a new view controller subclass.

Add > New File > UIViewController subclass with nib

on NavController.h

#import <UIKit/UIKit.h>


@interface NavController : UIViewController {

    IBOutlet UIWindow *window;
    IBOutlet UINavigationController *navCon;

}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navCon;

@end

on NavController.m

#import "NavController.h"


@implementation NavController

@synthesize window,navCon;


- (void)viewDidUnload {
    [super viewDidUnload];

}

- (void)dealloc {
    [window release];
    [navCon release];
    [super dealloc];
}


@end

i already dragged a Navigation Conrtoller and a Window on my IB, and connected window to window and the Navigation Controller to navcon outlets, but whats next?

Pfitz
  • 7,336
  • 4
  • 38
  • 51
Rowie Po
  • 1,339
  • 4
  • 13
  • 17

2 Answers2

3

If you're using the storyboards select your view controller then in top menu choose "editor" / "embed in" / "navigation controller".

moxy
  • 1,634
  • 1
  • 11
  • 19
0

Normally, you have to create an Navigationcontroller object inside your Appdelegate.h (like the existing window object). After that you import the h.File of a ViewController into the Appdelegate.m and init it like in the following example the menuviewcontroller. stackoverflow

To call another view use following lines of code, so the navigationcontroller will handle everything for you.

#import Viewcontroller

ViewControllerName controllerVarName = [ViewControllerName alloc] init];

[self.navigationcontroller pushViewController:_ViewControllerName animated:YES];

Inside your specific ViewController use this line to set the title the Navigationcontroller will use:

self.title = @"titleName";
Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • Thanks for the reply, in my appdelegate i added a UINavigationController. And i have a view controller with a login page, after login i added this: [appDelegate.viewController.view removeFromSuperview]; appDelegate.viewController.view = nil; [appDelegate.window addSubview:appDelegate.navigationController.view]; so it the view now changes to the navigation controller, i then created another view named "MainView", i pointed the navigation controller nib to the "MainView". So while in "MainView" how can i access the navigationcontroller? self.title doesn't work. – Rowie Po Jul 02 '12 at 08:36
  • sorry, forgot the self at the beginning of my code block. when initiating a new "subview" you push it through with this method. Everything else like getting back to the screen before will be handled by the navigationcontroller. To display the title of the called view just insert "self.title = @"titleName" " into ViewDidLoad() of the ViewController. You just have to sepcify every single view with this line. there are also methods that would do it dynamically but for beginning thats easier – Alex Cio Jul 02 '12 at 08:49
  • On my mainwindow.xib > navigationcontroller, i added a nib called "MainViewController", "MainViewController" is uiviewcontroller subclass, in "MainViewController.h" i added "self.title = @"titleName" but it does not seem to work. – Rowie Po Jul 02 '12 at 08:57