iPhone 6 Plus UISplitViewController与递归_canBecomeDeepestUnambiguousResponder

时间:2016-06-01 20:42:47

标签: ios swift uisplitviewcontroller iphone-6-plus

我有一个现有的iPhone应用程序,我正在添加UISplitViewController。 iPad部分就像一个魅力,但我保证iPhone 6(S)Plus的崩溃。

设置 - 主人是UITabBarController。初始细节是带有占位符徽标视图的视图。选择对象后,详细信息将替换为UITabBarController

每当我选择一个项目并打开iPhone 6 Plus中的细节并将其从纵向(仅详细可见)旋转到横向(主人可见的位置)时,它会崩溃。使用占位符详细信息视图旋转时不会发生这种情况。

在崩溃之前,它会调用委托方法primaryViewControllerForExpandingSplitViewControllersplitViewController(splitViewController: UISplitViewController, separateSecondaryViewControllerFromPrimaryViewController。但是,iPad上的一切正常。

我已经做了大量的搜索,只看到几个推特提到这种类型的崩溃。设置或不设置displayModeButtonItem之类的事情无济于事。

我在一个新项目中重新创建了这个崩溃 - 可以在这里下载: https://github.com/sschale/SplitViewCrash/

崩溃日志:

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00007fff53609ff8
Exception Note:        EXC_CORPSE_NOTIFY

VM Regions Near 0x7fff53609ff8:
    MALLOC_TINY            00007f8405000000-00007f8405300000 [ 3072K] rw-/rwx SM=PRV  
--> STACK GUARD            00007fff4fe0a000-00007fff5360a000 [ 56.0M] ---/rwx SM=NUL  stack guard for thread 0
    Stack                  00007fff5360a000-00007fff53dff000 [ 8148K] rw-/rwx SM=COW  thread 0

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0   liboainject.dylib              0x000000010e5e59b2
0   liboainject.dylib               0x000000010e5e59b2 

_writeEventToSharedMemory + 27
1   liboainject.dylib               0x000000010e5e55d7 _OARecordFinalEvent + 1161
2   liboainject.dylib               0x000000010e5e79f1 ___swapMethods_block_invoke_6 + 338
3   libobjc.A.dylib                 0x000000010f4f9b6b weak_read_no_lock + 89
4   libobjc.A.dylib                 0x000000010f4fa4c6 objc_loadWeakRetained + 104
5   com.apple.UIKit                 0x00000001110510b6 -[UIViewController presentedViewController] + 58
6   com.apple.UIKit                 0x0000000111033fc6 -[UIViewController _canBecomeDeepestUnambiguousResponder] + 31
7   com.apple.UIKit                 0x0000000111033fde -[UIViewController _canBecomeDeepestUnambiguousResponder] + 55
8   com.apple.UIKit                 0x0000000111033fde -[UIViewController _canBecomeDeepestUnambiguousResponder] + 55
9   com.apple.UIKit                 0x0000000111033fde -[UIViewController _canBecomeDeepestUnambiguousResponder] + 55
10  com.apple.UIKit                 0x0000000111033fde -[UIViewController _canBecomeDeepestUnambiguousResponder] + 55
//(500 more of those)
....

Thread 1:: Dispatch queue: com.apple.libdispatch-manager
0   libsystem_kernel.dylib          0x0000000116e49ee2 kevent64 + 10
1   libdispatch.dylib               0x0000000116ac57f0 _dispatch_mgr_invoke + 260
2   libdispatch.dylib               0x0000000116ac558a _dispatch_mgr_thread + 54

Thread 2:
0   libsystem_kernel.dylib          0x0000000116e495e2 __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x0000000116e0d578 _pthread_wqthread + 1283
2   libsystem_pthread.dylib         0x0000000116e0b341 start_wqthread + 13

Thread 3:
0   libsystem_kernel.dylib          0x0000000116e495e2 __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x0000000116e0d578 _pthread_wqthread + 1283
2   libsystem_pthread.dylib         0x0000000116e0b341 start_wqthread + 13

Thread 4:
0   libsystem_kernel.dylib          0x0000000116e495e2 __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x0000000116e0d578 _pthread_wqthread + 1283
2   libsystem_pthread.dylib         0x0000000116e0b341 start_wqthread + 13

Thread 5:
0   libsystem_kernel.dylib          0x0000000116e495e2 __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x0000000116e0d578 _pthread_wqthread + 1283
2   libsystem_pthread.dylib         0x0000000116e0b341 start_wqthread + 13

1 个答案:

答案 0 :(得分:5)

这也在iPad上崩溃了。使用Multitasking将应用程序调整为Compact宽度(例如1/3屏幕),按Launch Detail按钮,然后将其调整为Regular width。

当您处于紧凑宽度时,拆分视图控制器将“折叠”。这意味着它不再同时显示单独的主视图控制器和辅助视图控制器 - 而是将它们“折叠”为单个视图控制器层次结构。当它处于这种环境中时,通常需要你的帮助以便采取合理的行动。当主视图控制器和辅助视图控制器都是UINavigationControllers时,默认行为很有效,但在其他情况下则不行。

(在你的应用程序中,你的主要是一个UITabBarController,在你“启动详细信息”之后,次要也是一个UITabBarController。你可能想重新考虑那个设计,因为它会让事情变得更加困难。继续阅读。)< / p>

您的应用程序的“启动详细信息”按钮执行“显示详细信息”segue,它在UISplitViewController上有效地调用此方法:

public func showDetailViewController(vc: UIViewController, sender: AnyObject?)

请注意标题中的注释:

// In a horizontally-compact environment the master view controller
// or detail view controller is sent the showViewController:sender:
// message. If neither one of them provide an implementation for this
// method then it will fall back to a full screen presentation.

通过“主视图控制器或详细视图控制器”,它表示当前显示的视图控制器,在您的情况下是UITabBarController。但是UITabBarController没有为showViewController()实现任何东西,因为它没有足够的信息 - 它会在哪里显示视图控制器?它会添加新标签,还是替换旧标签,或者是什么?

因此,您可以获得后备全屏演示。我怀疑你真的想要那种用户体验。

稍后,当大小更改回Regular并且拆分视图控制器扩展时,它会被演示文稿混淆并最终崩溃。 (看看我的默认值不是很好的意思?)

解决此问题的一种方法是实现委托方法来处理showDetail。当宽度为Compact时,显式找到要将新视图控制器置于其上的视图控制器,并执行此操作。我想你可能想在第一个标签中推送导航控制器:

func splitViewController(splitViewController: UISplitViewController, showDetailViewController vc: UIViewController, sender: AnyObject?) -> Bool
{
    if splitViewController.traitCollection.horizontalSizeClass == .Compact {
        // The default implementation will not handle this properly.
        // Find the appropriate navigation controller and push onto it.
        // It would be better to have a direct outlet to the appropriate navigation controller, 
        // but this will work for an example...

        if let tabBarController = splitViewController.viewControllers.first as? UITabBarController {
            if let navController = tabBarController.viewControllers?.first as? UINavigationController {
                navController.pushViewController(vc, animated: true)

                // we handled the "show detail", so split view controller, 
                // please don't do anything else
                return true
            }
        }
    }

    // we did not handle the "show detail", so split view controller, 
    // please do your default behavior
    return false
}

如果这样做,您还需要实现此委托方法。当大小更改回常规时,您将需要通过从同一个导航控制器弹出该视图控制器来处理“展开”,然后返回它:

optional public func splitViewController(
    splitViewController: UISplitViewController
    separateSecondaryViewControllerFromPrimaryViewController
        primaryViewController: UIViewController) -> UIViewController?