在iOS 8中,视图控制器现在可以调用showDetailViewController:sender:
,让系统确定适当的视图控制器来显示细节视图控制器。
在我的应用程序中,我有一个UISplitViewController,它的viewControllers数组中包含两个UINavigationControllers。第一个UINavigationController包含我的“主”视图,它是UITableViewController的子类。第二个UINavigationController包含我的“detail”视图。
因为我试图让它通用工作,所以我尝试使用showDetailViewController:sender:
来显示细节视图:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
[self showDetailViewController:self.itemVC sender:self];
}
当为self.splitViewController.collapsed == YES
时,这与水平紧凑特征(iPhone样式)一起工作得很好,但当特征为常规(iPad,而不是折叠)时则不起作用。在iPad上,它用纯细节视图控制器替换细节UINavigationController (而不是替换UINavigationController的viewControllers数组)。
为了解决这个问题,我测试了它是否折叠,如果没有,我在显示它之前将细节视图控制器包装在另一个UINavigationController中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow;
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
vcToShow = self.itemVC;
} else {
vcToShow = [[UINavigationController alloc] initWithRootViewController:self.itemVC];
}
[self showDetailViewController:vcToShow sender:self];
}
我想,或者我可以只配置self.itemVC
,并在使用self.splitViewController.collapsed == NO
时完全避免调用showDetailViewController:sender:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
[self showDetailViewController:vcToShow sender:self];
}
}
但是,这似乎违背了showDetailViewController:sender:
的目的,即松散self
和视图层次结构的其余部分之间的耦合。
有没有更好的方法来处理这件事?
发布于 2014-09-13 06:08:03
在showDetailViewController:sender:
中,根据collapse
属性,您需要创建希望在细节中显示的控制器。
例如,在横向模式的iPad上,它已经从故事板创建了细节视图控制器,但在折叠它的iPhone 5上,视图控制器还不存在。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UINavigationController *detail;
ImageViewController *imageVC;
// on the iPhone (compact) the split view controller is collapsed
// therefore we need to create the navigation controller and its image view controllerfirst
if (self.splitViewController.collapsed) {
detail = [[UINavigationController alloc] init];
imageVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
[detail setViewControllers:@[imageVC] animated: NO];
}
// if the split view controller shows the detail view already there is no need to create the controllers
else {
id vc = self.splitViewController.viewControllers[1];
if ([vc isKindOfClass:[UINavigationController class]]) {
detail = (UINavigationController *)vc;
imageVC = [detail.viewControllers firstObject];
}
}
[self prepareImageViewController:imageVC forPhoto:self.photos[indexPath.row]];
// ask the split view controller to show the detail view
// the controller knows on iPhone and iPad how to show the detail
[self.splitViewController showDetailViewController:detail sender:self];
}
我希望这能解决你的问题。
发布于 2016-04-28 00:29:54
你做这件事的方式有问题。如果在选择后旋转设备(将模式从折叠模式更改为allVisible),您将发现没有导航控制器的detail vc。
如果您在所有情况下都调用showDetailViewController:sender:
,并将视图控制器与导航控制器一起传递给它,那么它在这两种情况下都可以很好地工作,并且还可以修复上面提到的旋转问题。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow= [[UINavigationController alloc] initWithRootViewController:self.itemVC];
[self showDetailViewController:vcToShow sender:self];
}
发布于 2014-12-04 03:31:46
if (self.splitViewController.collapsed)
[self.splitViewController showDetailViewController:self.itemVC sender:self];
else
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
https://stackoverflow.com/questions/25106548
复制相似问题