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):
    - (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

Be Sociable, Share!