问题描述
||
Apple在其iOS开发站点上提供了一个示例,说明如何将splitview与不同的detailviews一起使用(请参见MultipleDetailViews示例)。在选择RootViewController的时间内,将初始化一个新的DetailViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc] initWithNibName:@\"FirstDetailView\" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:@\"SecondDetailView\" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller\'s view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController,detailViewController,nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
// dismiss the popover if it\'s present.
if (popoverController != nil) {
[popoverController dismisspopoverAnimated:YES];
}
// Configure the new view controller\'s popover button (after the view has been displayed and its toolbar/navigation bar has been created).
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
}
现在,我的想法不是每次都初始化视图控制器,只需在viewDidLoad(以及标头)中初始化控制器,并在选择单元格时传递它们的引用:
rootviewcontroller中的viewDidLoad的一部分:
firstDetailViewController = [[FirstDetailViewController alloc] initWithNibName:@\"FirstDetailView\" bundle:nil];
secondDetailViewController = [[FirstDetailViewController alloc] initWithNibName:@\"FirstDetailView\" bundle:nil];
现在,tableView didselectrow方法看起来像这样:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (row == 0) {
detailViewController = firstDetailViewController;
}
if (row == 1) {
detailViewController = secondDetailViewController;
}
// Update the split view controller\'s view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController,nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
// dismiss the popover if it\'s present.
if (popoverController != nil) {
[popoverController dismisspopoverAnimated:YES];
}
// Configure the new view controller\'s popover button (after the view has been displayed and its toolbar/navigation bar has been created).
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
//[detailViewController release];
}
在横向模式下,一切正常,但是在纵向模式下,从第一个详细视图控制器切换到第二个,然后切换到第一个,然后又想切换到第二个时出现错误。出现以下消息:由于未捕获的异常'NSinvalidargumentexception \',终止了应用程序,原因:\'无法从没有窗口的视图中显示Popovers。\'
从昨天晚上开始,我试图找出答案,但是我没有找到解决方案。有人有主意吗?
干杯,多米尼克
解决方法
我有一个类似的问题,但是我发现这篇文章证明了您必须在设置splitView的视图控制器之前从以前的detailView中删除leftBarButtonItem的想法。这段代码为我解决了这个问题。
这是TPoschel编写的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Retrieve the new detail view controller
UIViewController *detailViewController = [[self.allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
// Add the detail view controller to a navigation controller and set the bar style
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
detailNavigationController.navigationBar.barStyle = [[NSUserDefaults standardUserDefaults] integerForKey:@\"UIBarStyle\"];
// Retrieve previous detail view controller and remove the leftBarButtonItem
UINavigationController *previousDetailNavigationController = [splitViewController.viewControllers objectAtIndex:1];
UIViewController *previousDetailViewController = [[previousDetailNavigationController viewControllers] lastObject];
previousDetailViewController.navigationItem.leftBarButtonItem = nil;
// Update the split view controller\'s view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController,detailNavigationController,nil];
splitViewController.viewControllers = viewControllers;
[detailNavigationController release];
[viewControllers release];
// Dismiss the popover if it\'s present.
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
// This sets the left bar to nil when in landscape and equal to \"Menu\" when in portrait.
// We need to remove rootPopoverButtonItem from the previous viewController...
detailViewController.navigationItem.leftBarButtonItem = rootPopoverButtonItem;
}