Saturday, May 15, 2010

Creating an UINavigationController application

A UINavigationController can be added to simple IPhone window application, to create a navigation bar on top of the view as shown in the diagram in the right. The following steps show how UINavigationController can be added to a simple IPhone window application:

1.  Add the UINavigationController object in the header file for the
     applicationDelegate.
     Eg. in ApplicationDelegate.h @interface section:
    UINavigationControoller *uiNavCtrl;

2. Add the following codes to the applicationDelegate.m main class in
    the didFinishedLaunchingWithOptions method.
    Eg.
         uiNavCtrl=[[UINavigationController alloc] init]; 
         [window addSubView: uiNavCtrl.view];   

3. After adding the above codes, UINavigationController has to be release in the dealloc method in the
    applicationDelegate.m main class.
     [uiNavCtrl    release];
    An UINavigationController can have multiple UIViewControllers class pushed into it. If more than one
   UIViewController are pushed into the UINavigationController, the last one that is pushed will be
   displayed, when the application is first launched. A Back button will be automatically created if there are
   more than one UIViewController class are pushed. Steps below outline how to add new
   UIViewController to UINavigationController:

4. If back button is needed in all subsequence pushed viewController (to return to parent view),
    the backBarButtonItem is needed to add to the root navigationController. If the title of the
    backBarButtonItem is not set, the default behaviour is that the back button will be created with
    the title of the parent view.
    **Note: For default behaviour to work, all the viewController must be set with a title.
    Eg:
              UIBarButtonItem *backButt = [[UIBarButtonItem alloc]init];
              [backButt setTitle:@"Back"];
              [self.navigationItem setBackBarButtonItem:backButt];

5. Create a new UIViewController sub-class.

    a) In XCode, under Groups & Files,
        expand current project. Higfhlight Class, right
        click Add->New File... Refer to the
        diagram in the left.
       A new file selection menu will be poped up.
   b) In the new File windows, select UIViewController sub-class.
       Also select with XIB for users interface option. This option
       will generate the XIB file automatically. This XIB is to be used in 
       the interface builder, to simplify the creation of visual control in the
      UIView. If this option is not selected, the XIB file has to be created
      manually later after the class file is generated. This option will
      save two steps of manual work to create the XIB file.
     After that click on the Next button, to proceed to next step of the
     class generation.

  c) In the following new File window, specify the name for the
     UIViewController sub-class that to be created. After that click the
     finish button to generate the class, header and XIB files.
     * Note: As part of the programming convension the class name
        typically start with uppercase character and follow with lowercase
        characters.
    Uppercase first character is also usually used for all the following
    words in the class name.
    Eg. FirstView , SecondView

6. Once the UIViewController sub-class is being generated, the view can be customized according to
    the requirement in the interface builder by clicking the newly created XIB file. The UI control (eg.
    UISlider, UIButton, UILabel... etc) can be added to the view in interface builder.

7. To use the newly created UIViewController in the window application, it has to be pushed to the
    UINavigationController. The best way is to push through the didFinishedLaunchingWithOptions
    method in the applicationDelegate.m main class.  Eg: add the following code to
    didFinishedLaunchingWithOptions in ApplicationDelegate.m, assume that FirstView is newly
    created UIViewController:
                   FirstView *fv = [[UIViewController alloc] init];
                   fv.title = @"Title";  // To add a title to the UIViewController
                  [uiNavCtrl    pushViewController: fv animated: NO];
                  [fv  release];
    *Note: If the animated option is set to YES, the UIView will be shown in a animated way.
    Typically, if it is push through the didFinishedLaunchingWithOptions method, the animation will not show.

8. The newly created UIViewController can also be pushed through any UI controller such as UIButton ...
    etc. IBAction method can be used to push the UIViewController. Eg. The following code show how this
    can be done:
    a) Add IBAction in the FirstView.h
          -(IBAction) getSecondView: (id) sender;
    b) Add IBAction in the FirstView.m in the @interface section:
         -(IBAction) getSecondView: (id) sender  {
                 SecondView *sv = [[SecondView   alloc] init];
                 [self.navigationController   pushViewController: sv animated: YES];
                 [sv release];
         }
    c) Link the UI component change event to the above created IBAction.

1 comment:

  1. Nice and very helpful information i have got from your post. Even your whole blog is full of interesting information which is the great sign of a great blogger.

    Lenovo - IdeaPad 15.6" Laptop - 6GB Memory - 1TB Hard Drive - Dark Chocolate

    Lenovo - 14" Notebook - 4 GB Memory - 320 GB Hard Drive - Black

    ReplyDelete