在我的应用程序中,我有一个UISplitViewController,它的viewControllers数组中包含两个UINavigationController.第一个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];
}
当使用“水平紧凑”特征(iPhone风格)时,如果self.splitViewController.collapsed == YES,但是当特征是常规(iPad,没有折叠)时,则可以正常工作.在iPad上,它使用裸露的细节视图控制器(而不是替换该UINavigationController的viewControllers数组)替换细节UINavigationController.
为了解决这个问题,我测试了它是否被折叠,如果没有,我将细节视图控制器包装在另一个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,并避免调用showDetailViewController:sender:一起当self.splitViewController.collapsed == NO:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
// For whatever reason,doesn't push onto it.
if (self.splitViewController.collapsed) {
[self showDetailViewController:vcToShow sender:self];
}
}
但是,这感觉像是打败了showDetailViewController:sender:的目的,这就是松开自己和其他视图层次结构之间的耦合.
有没有更好的办法呢?
解决方法
例如.在横向模式的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];
}
我希望这能解决你的问题.