UISplitViewController在iPhone上的纵向显示详细VC而不是主 [英] UISplitViewController in portrait on iPhone shows detail VC instead of master

查看:396
本文介绍了UISplitViewController在iPhone上的纵向显示详细VC而不是主的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 6中使用通用故事板,目标是iOS 7及更高版本。我已经实现了一个 UISplitViewController ,它现在在运行iOS 8的iPhone上原生支持,而Xcode会自动向iOS 7移植它。它工作得非常好,除非你启动它iPhone上的应用程序在运行iOS 8的肖像中,当我希望第一次看到主视图控制器时,会显示拆分视图的详细视图控制器。我相信这是iOS 8的一个错误,因为当您在iOS 7上运行应用程序时,它会正确显示主视图控制器。但iOS 8现在是通用汽车,而且这种情况仍在发生。如何进行设置以便在拆分拆分视图控制器时(屏幕上只显示一个视图控制器),当显示拆分视图控制器时,它会显示主视图控制器而不是详细信息?

I am using a Universal Storyboard in Xcode 6, targeting iOS 7 and above. I've implemented a UISplitViewController which is now natively supported on iPhone running iOS 8, and Xcode will automatically backport it for iOS 7. It's working really well, except when you launch the app on iPhone in portrait running iOS 8, the split view's detail view controller is displayed when I expected to first see the master view controller. I believed this was a bug with iOS 8 because when you run the app on iOS 7, it correctly shows the master view controller. But iOS 8 is now GM and this is still occurring. How can I set it up so that when the split view controller is going to be collapsed (only one view controller displayed on screen), when the split view controller is displayed it shows the master view controller not the detail?

我在Interface Builder中创建了这个拆分视图控制器。拆分视图控制器是标签栏控制器中的第一个视图控制器。主控制器和细节VC都是导航控制器,其中嵌入了表视图控制器。

I've created this split view controller in Interface Builder. The split view controller is the first view controller within a tab bar controller. Both the master and the detail VCs are navigation controllers with table view controllers embedded inside.

推荐答案

哦,伙计,这导致我几天头痛,无法弄清楚如何做到这一点。最糟糕的是,使用master-detail模板创建一个新的Xcode iOS项目工作正常。幸运的是,最后,我找到了解决方案的一个小事实。

Oh man, this was causing me a headache for a few days and could not figure out how to do this. The worst part was that creating a new Xcode iOS project with the master-detail template worked just fine. Fortunately, in the end, that little fact was how I found the solution.

我发现有一些帖子表明解决方案是实现新的 UISplitViewControllerDelegate 上的primaryViewControllerForCollapsingSplitViewController:方法。我试过没有用。 Apple在主要细节模板中做了什么似乎工作是实现新的(深呼吸说所有这一切) splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:委托方法(再次在 UISplitViewControllerDelegate )。根据 docs ,此方法:

There are some posts I've found that suggest that the solution is to implement the new primaryViewControllerForCollapsingSplitViewController: method on UISplitViewControllerDelegate. I tried that to no avail. What Apple does in the master-detail template that seems to work is implement the new (take a deep breath to say all of this one) splitViewController:collapseSecondaryViewController:ontoPrimaryViewController: delegate method (again on UISplitViewControllerDelegate). According to the docs, this method:


要求委托调整主视图控制器并合并辅助视图控制器进入折叠界面。

Asks the delegate to adjust the primary view controller and to incorporate the secondary view controller into the collapsed interface.

请务必阅读该方法的讨论部分以获取更具体的细节。

Make sure to read up on the discussion part of that method for more specific details.

Apple处理此问题的方式是:

The way that Apple handles this is:

- (BOOL)splitViewController:(UISplitViewController *)splitViewController
collapseSecondaryViewController:(UIViewController *)secondaryViewController
  ontoPrimaryViewController:(UIViewController *)primaryViewController {

    if ([secondaryViewController isKindOfClass:[UINavigationController class]]
        && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]]
        && ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {

        // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
        return YES;

    } else {

        return NO;

    }
}

此实现基本上执行以下操作:

This implementation basically does the following:


  1. 如果 secondaryViewController 是我们所期待的( UINavigationController ),它显示了我们期待的结果( DetailViewController - 您的视图控制器),但没有模型( detailItem ),然后返回YES表示我们通过什么都不做处理崩溃;辅助控制器将被丢弃。

  2. 否则,返回让分割视图控制器尝试将辅助视图控制器的内容合并到折叠界面中

  1. If secondaryViewController is what we're expecting (a UINavigationController), and it's showing what we're expecting (a DetailViewController -- your view controller), but has no model (detailItem), then "Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded."
  2. Otherwise, return "NO to let the split view controller try and incorporate the secondary view controller’s content into the collapsed interface"

iPhone的纵向结果如下(从纵向开始或纵向旋转 - 或者更准确地说是紧凑尺寸类别):

The results are the following for the iPhone in portrait (either starting in portrait or rotating to portrait -- or more accurately compact size class):


  1. 如果您的观点是正确的

    • 并且有一个模型,请显示详细视图C ontroller

    • 但没有型号,显示主视图控制器


  • 显示主视图控制器

清除泥浆。

这篇关于UISplitViewController在iPhone上的纵向显示详细VC而不是主的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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