0

我正在尝试以编程方式用 SplitViewController 替换当前视图。这是我的代码。

ProjectNavigationController *projectNavPanel = [[ProjectNavigationController alloc] init];
[projectNavPanel setProjectIndex:[indexPath row]];
[projectNavPanel setKuluId:[[[[[appDelegate userSettingsDictionary] objectForKey:@"Projects"] objectAtIndex:[indexPath row]] objectForKey:@"Kulu Id"] intValue]];

ProjectDetailController *projectDetailPanel = [[ProjectDetailController alloc] init];
[projectDetailPanel setProjectIndex:[indexPath row]];
ProjectSplitViewController *splitRootController = [[ProjectSplitViewController alloc] init];

[splitRootController setViewControllers:[NSArray arrayWithObjects:projectNavPanel, projectDetailPanel, nil]];

[[self view] removeFromSuperview];
[[appDelegate window] setRootViewController:splitRootController];

这几乎可以正常工作,但有一个例外。当拆分视图控制器加载并处于纵向模式时,导航视图会隐藏 - 正如预期的那样 - 但详细视图没有带有按钮的工具栏,用于在弹出视图中显示导航。根据我的研究,我知道这是默认行为。我错过了什么吗?detailViewController 中的代码很少,所以我没有在此处包含代码,但如果有帮助,请告诉我。

4

1 回答 1

1

您的代码中缺少一些东西;

首先,要让导航栏添加按钮,你必须UINavigationController在你的UISplitViewController和你的ProjectDetailController.

其次,您必须设置一个 UISplitViewControllerDelegate 并使用委托方法来添加按钮:

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{   
    [[projectDetailPanel navigationItem] setLeftBarButtonItem:barButtonItem animated:YES];
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    [[projectDetailPanel navigationItem] setLeftBarButtonItem:nil animated:YES];
}

第三,不要忘记将标题设置为 your ,或在您刚刚实现的方法中ProjectNavigationController手动设置标题。barButtonItemsplitViewController:willHideViewController:withBarButtonItem:forPopoverController

于 2013-04-24T15:04:13.327 回答