0

好的,所以我是一个 Master Detail 应用程序,当用户单击一个单元格时,它会在 DetailViewController 中加载一个网页。问题是,在 iPhone 版本(这是一个通用应用程序)上,我必须单击单元格,然后返回 MasterViewController,然后再次单击单元格以加载网页。我只需要这样做一次,我认为这就像第一次启动网页什么的。不过,在 iPad 版本上,它会第一次加载它。那么这是什么一回事?

这是我的 applicationDidFinishLaunching AppDelegate.m 文件的代码,我在其中初始化 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;
}

这是 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];
    }
}
4

1 回答 1

1

在 iPad 上,您在拆分视图控制器中有 2 个控制器,因此它们会被实例化,并且它们的视图会在应用程序启动时加载。在 iPhone 上,第二个控制器被实例化了,但是在你按下它之前它的视图不会被加载——我认为这就是区别。您应该尝试将 detailController 方法 loadURL: 移动到它的 viewDidAppear 方法,而不是将它放在主控制器的 didSelectRowAtIndexPath 方法中。

于 2013-02-01T20:44:16.407 回答