ios - 在 UISplitViewController 中,无法使 showDetailViewController :sender: push onto detail navigationController

标签 ios objective-c uinavigationcontroller uisplitviewcontroller ios8

在 iOS 8 中, View Controller 现在可以调用 showDetailViewController:sender: 让系统确定正确的 View Controller 来呈现细节 View Controller 。

在我的应用程序中,我有一个 UISplitViewController,它在其 viewControllers 数组中包含两个 UINavigationControllers。第一个 UINavigationController 包含我的“主” View ,它是 UITableViewController 的子类。第二个 UINavigationController 包含我的“详细信息” View 。

由于我试图使这项工作普遍适用,所以我尝试使用 showDetailViewController:sender: 来显示详细 View :

- (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 时,这适用于 Horizo​​ntal Compact 特征(iPhone 风格),但当特征为 Regular(iPad,未折叠)时则不然。在 iPad 上,它将细节 UINavigationController 替换为裸细节 View Controller (而不是替换 UINavigationController 的 viewControllers 数组)。

为了解决这个问题,我测试了它是否折叠,如果没有,我在显示它之前将细节 View Controller 包装在另一个 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, 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 与 View 层次结构的其余部分之间的耦合。

有没有更好的方法来处理这个问题?

最佳答案

showDetailViewController:sender: 中,根据 collapse 属性,您需要创建要在细节中显示的 Controller 。

例如在横向模式的 iPad 上,它已经从 Storyboard创建了细节 View Controller ,但在折叠模式的 iPhone 5 上, View Controller 尚不存在。

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

希望这能解决您的问题。

关于ios - 在 UISplitViewController 中,无法使 showDetailViewController :sender: push onto detail navigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106548/

相关文章:

ios - 如何查看 iTunes Connect 上的应用总安装量?

iphone - AVPlayerLayer 在 View 中不可见

ios - 单例实例自动释放的可能性

iOS 应用程序无法通过 OTA Enterprise Distribution 正确更新

ios - 我们可以在 iOS 中使用 Twilio api 做什么?

ios - 显示不同高度的 UIWebView 和 UITableView

ios - 在 iOS 7 中更改后退按钮会禁用滑动以返回导航

ios - 在 iOS 中的核心数据和 NSFetchedResultsController 中点击我的屏幕之前,reloadData 无法正常工作

iphone - 如何获取 UINavigationController 中使用的 UIView 原点的 x/y 坐标?

ios - 如何从 appDelegate 重新加载 rootViewController