在UISplitViewController中,无法使showDetailViewController:sender:推入详细信息navigationController [英] In UISplitViewController, can't make showDetailViewController:sender: push onto detail navigationController

查看:290
本文介绍了在UISplitViewController中,无法使showDetailViewController:sender:推入详细信息navigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8中,视图控制器现在可以调用showDetailViewController:sender:,以使系统确定合适的视图控制器来呈现详细视图控制器.

In iOS 8, view controllers can now call showDetailViewController:sender: to have the system determine the proper view controller to present the detail view controller.

在我的应用程序中,我有一个UISplitViewController,它的viewControllers数组中包含两个UINavigationControllers.第一个UINavigationController包含我的主"视图,它是UITableViewController的子类.第二个UINavigationController包含我的详细"视图.

In my app, I have a UISplitViewController, which contains two UINavigationControllers in its viewControllers array. The first UINavigationController contains my 'master' view, a subclass of UITableViewController. The second UINavigationController contains my 'detail' view.

由于我试图使这项工作通用,因此我尝试使用showDetailViewController:sender:显示详细视图:

Since I'm trying to make this work universally, I'm trying to use showDetailViewController:sender: to display the detail 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 trait(iPhone风格)一起正常使用,但在trait为Regular(iPad,未折叠)时则无法使用.在iPad上,它用裸露的细节视图控制器替换了细节UINavigationController(而不是替换了UINavigationController的viewControllers数组).

This works fine with the Horizontal Compact trait (iPhone style), when self.splitViewController.collapsed == YES, but not when the trait is Regular (iPad, not collapsed). On the iPad, it replaces the detail UINavigationController with the bare detail view controller (instead of replacing that UINavigationController's viewControllers array).

要解决此问题,我已经对其是否折叠进行了测试,如果没有折叠,则在显示它之前将详细视图控制器包装在另一个UINavigationController中:

To get around this, I'm tested for whether or not it's collapsed, and if it isn't, I'm wrapping the detail view controller in another UINavigationController before showing it:

- (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::

I suppose alternatively I could just configure self.itemVC and avoid calling showDetailViewController:sender: altogether when 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与视图层次结构其余部分之间的耦合.

But, this feels like it's defeating the purpose of showDetailViewController:sender:, which is to loosen up the coupling between self and the rest of the view hierarchy.

有没有更好的方法来解决这个问题?

Is there a better way to handle this?

推荐答案

showDetailViewController:sender:中,根据collapse属性,您需要创建要在详细信息中显示的控制器.

In showDetailViewController:sender: depending on the collapse property you need to create the controller you want to show in the detail.

例如在横向模式下的iPad上,它已经可以从情节提要中创建细节视图控制器,但是在折叠起来的iPhone 5上,该视图控制器尚不存在.

E.g. On the iPad in landscape mode it would already create the detail view controller from the storyboard but on the iPhone 5 where it is collapsed the view controller does not exist yet.

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

我希望这能解决您的问题.

I hope this solves your issue.

这篇关于在UISplitViewController中,无法使showDetailViewController:sender:推入详细信息navigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆