Sunday, 23 February 2014

iPhone Development:Add Navigation controller to single viewController template in Xcode

In this post i am going to tell you how could you add a NavigationController(used in iPhone Objective C) to navigate from one viewController to other viewController in iPhone when you chose single view template in xcode.
Suppose you are creating a new project in xcode and you have chosen singleview template as your starting template and after making some inroads you reliazed that you needed navigationcontroller template at the starting of your project so that you can navigate in your apps with default push or pop animations.
No worries as i am going to tell you today how you can fix this by making few changes in your AppDelegate files.
Select your AppDelegate.h and look at it carefully, you will see a viewcontroller instance.
@property (strong, nonatomic) ViewController *viewController;

Now select AppDelegate.m and looking at it you will realize that your viewcontroller instance was given as rootviewController to your window.
  self.window.rootViewController = self.viewController;

Get ready for some excitement now, to make single view template as navigation template. Open your AppDelegate.h file and create an instance of UINavigationController as given below.
@property (strong, nonatomic) UINavigationController *navController;

As you declared your UINavigatonController instance, go to applicatuondidFilnishLaunching methods of AppDelagte.m class and alloc init your UINavigationController instance, and set your viewController as its rootViewController.The code is shown below
 NSMutableString *xibString = [[NSMutableString alloc]initWithCapacity:0];
    if ([UIScreen mainScreen].bounds.size.height>480.0f)
        [xibString appendString:@"ViewController"];
        [xibString appendString:@"ViewController_Low"];
    self.viewController = [[[ViewController alloc] initWithNibName:xibString bundle:nil] autorelease];
    [xibString release];
    xibString = nil;
    self.navController = [[[UINavigationController alloc]initWithRootViewController:self.viewController] autorelease];

Now the last step is to replace viewcontroller as your window's rootviewController and in place of that give navigationcontrooller.
   self.window.rootViewController = self.navController;

That's it your are done with it and now you have navigationcontroller template in place of single view.

No comments:
Write comments