Tutorial: Turning A View-based Into A Navigation-based Project

October 1st, 2011 Leave a comment Go to comments

This tutorial was written using Xcode 4.0.

Suppose you have a project called MyApp. It was created using the general View-based application, but now you want to turn it into navigation-based project. Here’s how you do it.

  1. In MyAppAppDelegate.h, change the type of the viewController outlet from MyAppViewController to UINavigationController.

  2. In MainWindow.xib, delete the My App View Controller

  3. Add a Navigation Controller from the Object library.

  4. Ctrl-click and drag MyApp App Delegate to the Navigation Controller object, select the viewController outlet.

  5. Select the View component in the edit area, enter MyAppViewController as the NIB Name in the Attributes inspector.

If you want a table view as the application’s root view, follow these additional steps.

  1. In MyAppViewController.h, change the ancestor of MyAppViewController from UIViewController to UITableViewController


  2. In MyAppViewController.xib, delete the existing View object.


  3. Add a Table View from the Object library.


  4. Ctrl-click and drag the Files owner object to the Table view object, select the view outlet.

  5. Ctrl-click and drag the Table view object to the Files owner object to connect the dataSource and the delegate outlets. (Note that you need to drag twice)


  6. In MinWindow.xib, select the Navigation Controller, then select the View in the edit area (the control that says Loaded from “MyAppViewController”).
    In the identity inspector, change the Class from UIViewController to MyAppViewController.


    Note that failing to do the last step will result in this runtime exception:

    -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance messages.

  7. Finally, in MyAppViewController.m, replace the code within the @implementation section with the following (which is the same code that the Navigation-based template adds):
    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    - (void)viewDidLoad
    {
      [super viewDidLoad];
    }
     
    - (void)viewWillAppear:(BOOL)animated
    {
      [super viewWillAppear:animated];
    }
     
    - (void)viewDidAppear:(BOOL)animated
    {
      [super viewDidAppear:animated];
    }
     
    - (void)viewWillDisappear:(BOOL)animated
    {
      [super viewWillDisappear:animated];
    }
     
    - (void)viewDidDisappear:(BOOL)animated
    {
      [super viewDidDisappear:animated];
    }
     
    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      // Return YES for supported orientations.
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */
     
    // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      return 1;
    }
     
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return 0;
    }
     
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      }
      // Configure the cell.
      return cell;
    }
    /*
     
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // Return NO if you do not want the specified item to be editable.
      return YES;
    }
    */
     
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
      if (editingStyle == UITableViewCellEditingStyleDelete)
      {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      }
      else if (editingStyle == UITableViewCellEditingStyleInsert)
      {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
      }
    }
    */
     
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
     
    }
    */
     
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // Return NO if you do not want the item to be re-orderable.
      return YES;
    }
    */
     
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      /*
      <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
      // ...
     
      // Pass the selected object to the new view controller.
      [self.navigationController pushViewController:detailViewController animated:YES];
      [detailViewController release];
      */
    }
     
    - (void)didReceiveMemoryWarning
    {
      // Releases the view if it doesn't have a superview.
      [super didReceiveMemoryWarning];
      // Relinquish ownership any cached data, images, etc that aren't in use.
    }
     
    - (void)viewDidUnload
    {
      [super viewDidUnload];
      // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
      // For example: self.myOutlet = nil;
    }
     
    - (void)dealloc
    {
      [super dealloc];
    }

    Now you’re done transforming your view-based application into a navigation-based one.
    Cheers