I am using a split view controller and have an issue when replacing my detail view controller with a new view controller. When I start the app I get my empty view controller as my detail view controller and hit the button on the navigation bar to show the master view controller. When I select a row in master view controller my detail view controller is replaced with the appropriate controller. It works fine the first time you select a row, but every time after that, when a row is selected from the master controller, the master controller doesn't go away so it just stays on top of the detail controller.
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterVC = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
UINavigationController *masterNavController = [[UINavigationController alloc]initWithRootViewController:masterVC];
DetailViewController *detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
UINavigationController *detailNavController = [[UINavigationController alloc]initWithRootViewController:detailVC];
masterVC.detailViewController = detailVC;
self.splitViewController = [[UISplitViewController alloc]init];
self.splitViewController.delegate = detailVC;
self.splitViewController.viewControllers = @[masterNavController, detailNavController];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}
MasterViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ActionAlertsViewController *rootView = [[ActionAlertsViewController alloc]initWithNibName:nil bundle:nil];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy];
if (indexPath.section == 0) {
if (indexPath.row == 0) {
rootView.title = @"Action Alerts";
rootView.background = [UIImage imageNamed:@"capitol"];
rootView.urlString = @"http://kyfbnewsroom.com/category/public-affairs/notifications/feed/";
[rootView fetchEntries];
UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:rootView];
if (details.count > 1) {
[details replaceObjectAtIndex:1 withObject:detailNav];
} else {
[details insertObject:detailNav atIndex:1];
}
appDelegate.splitViewController.viewControllers = details;
appDelegate.window.rootViewController = self.splitViewController;
appDelegate.splitViewController.delegate = rootView;
[appDelegate.splitViewController viewWillAppear:YES];
}
}
}
I don't know where to start:
UIViewControllers
: [self.splitViewController.viewControllers mutableCopy]
[appDelegate.splitViewController viewWillAppear:YES]
appDelegate.window.rootViewController
AppDelegate
to find out which view you areviewWillAppear
that is a view controller lifecycle method that is called by the system. You should say something like [self.splitViewController showDetailViewController:detailNav sender:self]
Do read the documentation and follow the tutorials: