替换细节视图控制器

时间:2016-02-02 16:15:50

标签: ios objective-c uisplitviewcontroller

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

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

1 个答案:

答案 0 :(得分:1)

我不知道从哪里开始:

  1. 不要复制/克隆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]
  6. 之类的东西

    请阅读documentation并按照教程进行操作: