6

I am new to iOS. And I want to use navigation controller in my application but I have no any idea how to do it. So can any one guide me step by step for creating navigation in my application.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3418619
  • 235
  • 1
  • 2
  • 14
  • have u used in stroyborad or xib in your xcode – Anbu.Karthik Apr 10 '14 at 08:05
  • [Navigation controller Programetically](http://www.idev101.com/learn/navigation_controllers.html) [Navigation controller in storyboard](http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/) – Mayank Jain Apr 10 '14 at 07:42

7 Answers7

15

In appDelegate.h

@property (strong, nonatomic) UINavigationController *navController;

and set the delegate UINavigationControllerDelegate and synthesise object in appDelegate.m now,

appDelegate.m

you can set navigation controller in didFinishLaunchingWithOptions method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    frstVwCntlr = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
    self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
    self.window.rootViewController = self.navController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

In the above code , your firstViewController is set to UINavigationController and UINavigationController added to UIWindow like

self.window.rootViewController = self.navController

Hope this may help you

Community
  • 1
  • 1
Bhanu
  • 1,249
  • 10
  • 17
3

If you want to create everything programmatically you have to do it in AppDelegate.

But if you don't want to do it programmatically, then just select the ViewController in Storyboard then select menu options:

Editor > Embed In > Navigation Controller

pkamb
  • 33,281
  • 23
  • 160
  • 191
Rukshan
  • 7,902
  • 6
  • 43
  • 61
1

You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.

iDeveloper
  • 223
  • 2
  • 5
1

So for creating a UINavigationController programatically without using storyboards, go to your app delegate and do the following. Create two properties, window and viewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor=[UIColor clearColor];

    self.viewController = [[YourFirstViewController alloc] initWithNibName:@"YourFirstViewController" bundle:nil];
    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];

    // Override point for customization after application launch.
    return YES;
}
Satheesh
  • 10,998
  • 6
  • 50
  • 93
1
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];

where ImageViewController2 is a class name

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
user3418619
  • 235
  • 1
  • 2
  • 14
0

Here is the code that you should write in app delegate.

UIViewController *vc=[[UIViewController alloc]initWithNibName:@"vc1" bundle:nil];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
view.backgroundColor=[UIColor redColor];
[vc setView:view];

self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = self.navme;
Robert
  • 5,278
  • 43
  • 65
  • 115
0

For Swift 3.0, using filter:

let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)
Ved Rauniyar
  • 1,539
  • 14
  • 21