替换细节视图控制器

Replacing Detail View Controller

我使用的是拆分视图控制器,在用新的视图控制器替换细节视图控制器时遇到问题。当我启动应用程序时,我将我的空视图控制器作为我的详细视图控制器,然后点击导航栏上的按钮以显示主视图控制器。当我 select 主视图控制器中的一行时,我的详细视图控制器将替换为适当的控制器。第一次 select 一行时它工作正常,但之后每次,当从主控制器 select 编辑一行时,主控制器不会消失,所以它只是停留在顶部细节控制器。

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;
}

主视图控制器:

- (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];
        }
    }
}

我不知道从哪里开始:

  1. 不要 copy/clone UIViewControllers: [self.splitViewController.viewControllers mutableCopy]
  2. 不要调用系统启动的消息:[appDelegate.splitViewController viewWillAppear:YES]
  3. 不要重置根视图控制器"appDelegate.window.rootViewController
  4. 不要回到 AppDelegate 看看你是哪个视图
  5. @beyowulf 指出:您不应该调用 viewWillAppear 这是系统调用的视图控制器生命周期方法。你应该说 [self.splitViewController showDetailViewController:detailNav sender:self]

一定要阅读 documentation 并遵循教程: