UiSplitViewController segue导致Detail替换左侧的Master

时间:2019-03-12 01:39:53

标签: ios swift uisplitviewcontroller

我在iPad上的UiSplitViewController segue存在问题。该实现或多或少是标准的:一个初始的拆分视图控制器,该控制器连接到主VC和明细VC,每个VC之前都有导航控制器。问题在于,当选择主VC表中的项目时,明细VC会替换拆分视图左侧的主VC,而不仅仅是更新右侧的明细VC。

我试图将我能做的一切与Xcode 10.1生成的Master-Detail应用程序模板中的新虚拟应用程序进行比较。我没有发现任何实质性差异,也无法找出其他地方寻找问题的根源。

说明问题:

Here's what the normal starting point looks like.

Here's the bad result of selecting a table view item.

Here's a picture of the outlet wiring.

在我的AppDelegate.swift文件中,与模板的主要区别在于,我具有特殊的外壳,可以容纳首次使用时的模式入门屏幕:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let splitViewController = self.window!.rootViewController as! UISplitViewController
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
    splitViewController.delegate = self

    if splitViewController.isCollapsed == false {
        //if in splitView and portrait, force this VC to become visible
        //So that MyNetworkVC runs and can trigger Onboarding
        if splitViewController.displayMode == .primaryHidden {
            splitViewController.preferredDisplayMode = .primaryOverlay //.allVisible
        }
    }
    return true
}

[Snip ...]

func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
    guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
    guard let topAsDetailController = secondaryAsNavController.topViewController as? IntroDetailsVC else {
        return false
    }
    if topAsDetailController.selectedContact == nil {
        // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
        return true
    } else {
        return false
    }
}

最后,在我的主VC中:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "IntroDetails" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let iDVC = (segue.destination as! UINavigationController).topViewController as! IntroDetailsVC
            iDVC.selectedContact = fetchedResultsController.object(at: indexPath)
            iDVC.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
            iDVC.navigationItem.leftItemsSupplementBackButton = true

            //set the default selection to the first item so split view has something to display
            //FIXME: Save last selection to user defaults and bring it back when we re-appear
            if iDVC.selectedContact == nil {
                if let numRecords = fetchedResultsController.fetchedObjects?.count {
                    if numRecords > 0 {
                        let initialIndexPath = IndexPath(row: 0, section: 0)
                        iDVC.selectedContact = fetchedResultsController.object(at: initialIndexPath)
                    }
                }
            }
        }
    }
}

这是我看不到的问题吗?还是应该在其他地方寻找问题的根源?

0 个答案:

没有答案