Well, might not be clear with the title. I have pulled this right out of the MultipleDetailView sample code from Apple. Every time the user selects a row from the table in the pop over, detailViewController is allocated the FirstDetailViewController and SecondDetailViewController again. Instead of allocating and initializing the view controller over and over, I want to assign the existing and already allocated and initialized view controller if existing to the detailViewController on the selection of the row. I have modified the Split View Template instead of the sample code to achieve what I need. Code from the program:
This is the AppDelegate.h file:
@interface iPadHelloWorldAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UISplitViewController *splitViewController;
MasterViewController *masterViewController;
DetailViewController *detailViewController;
SecondDetailViewController *secondDetailViewController;
}
This is the AppDelegate.m file:
masterViewController = [[MasterViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
secondDetailViewController = [[SecondDetailViewController alloc] initWithNibName:@"SecondDetailView" bundle:nil];
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
splitViewController.delegate = detailViewController;
// Add the split view controller's view to the window and display.
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];
This is the MasterViewController.m:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = indexPath.row;
[self.appDelegate.splitViewController viewWillDisappear:YES];
self.tempArrays = [NSMutableArray arrayWithArray:self.appDelegate.splitViewController.viewControllers];
[self.tempArrays removeLastObject];
if (row == 0) {
[self.tempArrays addObject:self.appDelegate.detailViewController];
self.appDelegate.splitViewController.delegate = self.appDelegate.detailViewController;
}
if (row == 1) {
[self.tempArrays addObject:self.appDelegate.secondDetailViewController];
self.appDelegate.splitViewController.delegate = self.appDelegate.secondDetailViewController;
}
self.appDelegate.splitViewController.viewControllers = self.tempArrays;
[self.appDelegate.splitViewController viewWillAppear:YES];
}
This is the DetailViewController.m:
#pragma mark -
#pragma mark Split view support
- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
barButtonItem.title = @"Master List";
[navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
self.popoverController = pc;
}
// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
[navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
self.popoverController = nil;
}
I am able to lazy load the view controllers, but when I tap the bar button for the popover and jump to the second view controller, the second view controller does not show the pop over. When I jump back to the first detail view controller, the popover is displayed.
Basically, here is a similar question. But the link to the drop box there doesn't work.