Update DetailViewController from RootController

I'm trying to create an iPad app with a user interface similar to the Apple Mail app, namely:

  • The RootView controller (table view) on the left side of the split view for navigation with a hierarchy of multiple views. When a table cell is selected, a new table view is pushed to the left.
  • A new view on the left can update the detailed view.

I can accomplish both tasks, but not together. I mean, I can create a layered table view in RootController.

Or I can make a sibling table view in a RootController that can update the detailViewController.

Can someone tell me how to make a multi-level table in a RootController that can update the detailViewController?

There is more source code by reference, but below is a method in which I assume I should declare a new detailViewController (which should be placed in the UISplitViewController):

- (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; //Get the children of the present item. NSArray *Children = [dictionary objectForKey:@"Children"]; // if([Children count] == 0) { /* Create and configure a new detail view controller appropriate for the selection. */ NSUInteger row = indexPath.row; UIViewController <SubstitutableDetailViewController> *detailViewController = nil; if (row == 0) { FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc]initWithNibName:@"FirstDetailView" bundle:nil]; detailViewController = newDetailViewController; } if (row == 1) { SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc]initWithNibName:@"SecondDetailView" bundle:nil]; detailViewController = newDetailViewController; } // Update the split view controller view controllers array. NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil]; splitViewController.viewControllers = viewControllers//nothing happens..... [viewControllers release];// } else { //Prepare to tableview. RootViewController *rvController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]]; //Increment the Current View rvController.current_level += 1; //Set the title; rvController.current_title = [dictionary objectForKey:@"Title"]; //Push the new table view on the stack [self.navigationController pushViewController:rvController animated:YES]; rvController.tableDataSource = Children; [rvController.tableView reloadData]; //without this instrucion,items won't be loaded inside the second level of the table [rvController release]; } } 
+4
source share
1 answer

Sorry, but I can’t post my source code because it contains confidential information. When I have more free time, I will create a separate project and upload the code somewhere.

Here are excerpts from how I have done so far (welcome any feedback).

RootViewController - Note I have 4 partitions in my root table.

 #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Detail view logic NSUInteger section = indexPath.section; UIViewController <SubstitutableDetailViewController> *detailViewController = nil; if (section == 2) { ProductSearchDetailView *viewController = [[ProductSearchDetailView alloc] initWithNibName:@"ProductSearchDetailView" bundle:nil]; detailViewController = viewController; //[viewController release]; } else { DetailViewController *defaultDetailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil]; detailViewController = defaultDetailViewController; //[defaultDetailViewController release]; } // Navigation logic switch (section) { case 0: { break; } case 1: { break; } case 2: { // new Navigation view ProductSearchViewController *viewController = [[ProductSearchViewController alloc] initWithNibName:@"ProductSearchViewController" bundle:nil]; viewController.navigationItem.backBarButtonItem.title = @"Back"; [self.navigationController pushViewController:viewController animated:YES]; [viewController release]; break; } case 3: { StoreLocatorNavController *viewController = [[StoreLocatorNavController alloc] initWithNibName:@"StoreLocatorNavController" bundle:nil]; viewController.navigationItem.backBarButtonItem.title = @"Back"; [self.navigationController pushViewController:viewController animated:YES]; [viewController release]; break; } } // Update the split view controller view controllers array. NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil]; splitViewController.viewControllers = viewControllers; [viewControllers release]; // Dismiss the popover if it present. if (popoverController != nil) { [popoverController dismissPopoverAnimated:YES]; } // Configure the new view controller popover button (after the view has been displayed and its toolbar/navigation bar has been created). if (rootPopoverButtonItem != nil) { [detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem]; } [detailViewController release]; } 

NSNotificationCenter Part

Add this to the ProductSearchViewController:

 #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *itemAtIndex = (NSDictionary *)[self.productResults objectAtIndex:indexPath.row]; [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProduct" object:itemAtIndex]; } 

And finally add this to ProductSearchDetailViewController:

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheProductDetails:) name:@"updateProduct" object:nil]; } - (void)updateTheProductDetails:(NSNotification *)notification { NSDictionary *productDictionary = [NSDictionary dictionaryWithDictionary:[notification object]]; // product name _productName.text = [productDictionary objectForKey:@"ProductDescription"]; } 

Hope this helps!

0
source

All Articles