Search This Blog

Monday, November 22, 2010

Adding new view to RootViewController in UISplitViewController

  1. Right click the blue icon in Xcode.
  2. Add new file > Cocoa Touch Classes > UIViewController subclass.
  3. Select all 3 checkbox options (Targetted for iPad, UITableViewController subclass, with XIB for user interface) (You could rearrange the file in correct locations under classes and resources).
  4. Open NewView.h. Add NSArray variable for populating the UITableView.
  5. Open NewView.m. Edit numberOfSectionsInTableView, numberOfRowsInSectioncellForRowAtIndexPath to populate the table form the NSArray variable.
  6. Open RootViewController.h. Add #import "NewView.h".
  7. Open RootViewController.m. Edit

    - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NewView * NewView = [[NewView alloc] initWithNibName:@"NewView" bundle:nil];
    NewView.arrData = [[dataHolder objectAtIndex:indexPath.row] objectForKey:@"subdata"];
    [[NewView navigationItem] setTitle: @"Subdata"];
    [self.navigationController pushViewController:NewView animated:YES];
    [NewView release];
    /* When a row is selected, set the detail view controller's detail item to the item associated with the selected row.*/
    detailViewController.detailItem = [dataHolder objectAtIndex:indexPath.row];
    }
    Save and Run.

When an item is selected from the main root view, the table will slide to left to give way to your new view, with a back button. If the RootViewController is given a title, the title will be used for the back button.

To provide a title:
Open RootViewController.m In viewDidLoad, add
[self.navigationItem setTitle: @"Main View"];

No comments: