Search code examples
iosxcodeuiviewcontrolleruiwebviewuisplitviewcontroller

DetailViewController only loading webpage on second time


Ok, so I a a Master Detail application, and when a user clicks on a cell, it loads loads a webpage in the DetailViewController. The problem is, that on the iPhone version (this is a universal app) I have to click on the cell, then go back to the MasterViewController and then click on the cell again to load webpage. I only have to do this once, I think it is like initiating the webpage or something the first time. On the iPad version though, it loads it one the first time. So what's up?

Here is the code for my applicationDidFinishLaunching AppDelegate.m file, where I init the DetailViewController

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController_iPhone" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
    self.window.rootViewController = self.navigationController;
    masterViewController.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:nil];
} else {
    MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController_iPad" bundle:nil];
    UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:nil];
    UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];

    masterViewController.detailViewController = detailViewController;

    self.splitViewController = [[UISplitViewController alloc] init];
    self.splitViewController.delegate = detailViewController;
    self.splitViewController.viewControllers = @[masterNavigationController, detailNavigationController];

    self.window.rootViewController = self.splitViewController;
}

here is the code for the didSelectCellAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *object = _objects[indexPath.row];
    NSURL *ex = [NSURL URLWithString:[object objectForKey:@"url"]];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.detailViewController.detailItem = object;
        [self.detailViewController loadURL:ex];
        self.detailViewController.detailItem = object;
        [self.detailViewController loadURL:ex];
        [self.navigationController pushViewController:self.detailViewController animated:YES];
    } else {
        self.detailViewController.detailItem = object;
        [self.detailViewController loadURL:ex];
    }
}

Solution

  • On the iPad, you have the 2 controllers in a split view controller, so they are instantiated, and their view's loaded when the app starts up. On the iPhone, that second controller is instantiated, but it's view isn't loaded until you push it -- I think that's the difference. You should try moving the detailController method loadURL: to it's viewDidAppear method, instead of having it in the master controller's didSelectRowAtIndexPath method.