Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift splitview控制器显示详细视图,即使该视图为空_Ios_Swift_Uisplitviewcontroller - Fatal编程技术网

Ios swift splitview控制器显示详细视图,即使该视图为空

Ios swift splitview控制器显示详细视图,即使该视图为空,ios,swift,uisplitviewcontroller,Ios,Swift,Uisplitviewcontroller,我使用的是分割视图控制器。当我在某些设备中启动它时,主视图被隐藏,只显示细节。详细信息为空,因为尚未在主控中选择行 因此,我需要以下解决方案之一: 1) 将局部视图默认为主视图中的第一个项目 2) 自动显示主视图,或者通过某种方式使其可见 它使用的是导航栏中的自动[

我使用的是分割视图控制器。当我在某些设备中启动它时,主视图被隐藏,只显示细节。详细信息为空,因为尚未在主控中选择行

因此,我需要以下解决方案之一:

1) 将局部视图默认为主视图中的第一个项目

2) 自动显示主视图,或者通过某种方式使其可见


它使用的是导航栏中的自动[正如其他人所共享的,这与展开段无关

如果查看由主详图模板生成的AppDelegate.swift代码,您将看到此方法,该方法确定折叠时是否显示详图视图:

func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController:UIViewController!, ontoPrimaryViewController primaryViewController:UIViewController!) -> Bool {
    if let secondaryAsNavController = secondaryViewController as? UINavigationController {
        if let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController {
            if topAsDetailController.detailItem == nil {
                // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
                return true
            }
        }
    }
    return false
}
示例代码正在检查详细视图控制器的
detailItem
属性,以查看它是否有任何详细信息。如果是,则折叠时显示局部视图,否则显示主视图

您必须修改此代码以检查您正在使用的特定属性,该属性包含主控形状将在其“showDetail”
prepareForSegue
中传递给细节的细节项


完成此操作后,如果局部视图为空,则在折叠时不会显示该视图。

加载时,我能够将第一行项目放入局部视图。这里是主控的viewDidLoad,它被调用,即使它没有显示给用户

override func viewDidLoad() {
    getItems() // gets the items from the web service

    if let split = self.splitViewController{
        let controllers = split.viewControllers
        self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController

        // this line sets the "default" item
        self.detailViewController?.detailItem = items.items[0]
    }
}

现在,在用户登录并显示详细信息视图后,它已经填充了第一个项目。

我遇到了同样的问题,并找到了附加的解决方案。也许是为了一些帮助

显示的splitViewController类与情节提要中的splitViewController链接

//
//  InfoMainSplitViewController.swift
//

import UIKit

class InfoMainSplitViewController: UISplitViewController, UISplitViewControllerDelegate {

    // MARK: - Global variables
    // variable to control if the detail view should be collapsed on launch
    var forceDetailToColapse : Bool = false


    // MARK: - Lifetime management
    override func viewDidLoad() {
        super.viewDidLoad()
        // set the delegate to get access to the delegate methods
        self.delegate = self

        // if this is an iPad, show both scenes (selection and detail) side by side
        if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {

            // we have an iPad, so set the mode
            self.preferredDisplayMode = UISplitViewControllerDisplayMode.allVisible

            // we do not want to collapse the detail view on launch
            forceDetailToColapse = false

        } else {

            // we have an iPhone, set the mode
            self.preferredDisplayMode = UISplitViewControllerDisplayMode.automatic

            // make sure we collapse the detail view on launch
            forceDetailToColapse = true
        }
    }


    // MARK: - Delegate methods
    // used to collapse the detail view
    // BTW: this method will not be called if preferredDisplayMode == UISplitViewControllerDisplayMode.allVisible
    func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {

        // true: detail view will collapse, false: detail View will not collapse
        return forceDetailToColapse 
    }
}

你的问题与放松关系无关(你不能放松关系)。您应该编辑您的问题以给它一个更好的标题,并删除unwind segue标记。您需要阅读并理解如何使用UISplitViewController协议自定义SplitViewControllerDelegate的行为。我尝试更改此代码,但对视图没有明显影响。它仍然会弹出,只打开局部视图。因此,也许最好看看如何以编程方式选择主视图中的第一个项目,以便在启动时将其加载到详细视图中?如果它不工作,则会出现问题。这将是很好的尝试跟踪它,以了解问题,并修复它,使您的应用程序正常工作。您是否设置了断点,以验证是否正在调用它?如果不是,您的AppDelegate中是否仍有
splitViewController.delegate=self
行?您是否更改了故事板场景的任何内容,或者在未满足条件的情况下,通过代码查看哪个
?启动时显示DV的另一个问题是DVC的委托为零,因此除非您也设置了委托,否则无法保存DV中输入的任何数据。你会怎么处理?