ios - 多个 UISplitView 的一个详细 View

标签 ios swift uisplitviewcontroller

我有 3 个具有不同主视图的 UISplitViewController,但它们具有相同的详细 View 。所有这些都在 Storyboard 中连接。

所有 UISplitViewController 都嵌套在 UITabBarViewController 中,所以我通过标签栏项在它们之间切换。

问题是,当我切换到另一个选项卡(另一个 UISplitViewController)时,详细 View 消失了,我只看到主视图,详细 View 的位置充满了深灰色(见图)。我不想在切换后重新加载详细 View ,只需将其保留在屏幕右侧即可。

enter image description here

我不确定我需要提供什么代码,所以如果你需要任何代码,请询问,我会把它添加到问题中。

感谢您的帮助!

最佳答案

原因

我的第一个假设是,如果您在两个不同的 UISplitViewController 之间共享一个详细 View Controller ,这对应于 UITabController 的两个选项卡,两个单独的详细 View Controller 被创建。 具有此布局的测试项目证实了这一点:

enter image description here

Root View Controller 是一个 DetailViewController。当我在 viewDidLoad(_:) 中放置一个断点时,它被命中两次并且打印显示创建了两个不同的 DetailViewController 实例:

(lldb) po self
<TestTabSplit.DetailTableViewController: 0x7fbd10eb9cd0>

(lldb) po self
<TestTabSplit.DetailTableViewController: 0x7fbd10ebc700>

解决方案

使用共享容器 View Controller 作为两个 UISplitViewController 的详细 View Controller 。

您的新 Storyboard布局将如下所示:

enter image description here

  1. 为您的详细 View Controller (在本例中为导航 Controller )提供 Storyboard ID:

enter image description here

  1. 接下来,在您的应用委托(delegate)中,实例化详细 View Controller :

    // Add a variable to reference from elsewhere.
    var sharedNavigationController: UINavigationController!
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
    
        sharedNavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SharedID") as! UINavigationController
        return true
    }
    
  2. 最后,容器 View Controller ContainerViewController 只是 UIViewController 的子类,具有以下内容:

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let sharedNavigationController = appDelegate.sharedNavigationController
    
        addChildViewController(sharedNavigationController)
        sharedNavigationController.view.frame = view.bounds
        view.addSubview(sharedNavigationController.view)
        sharedNavigationController.didMoveToParentViewController(self)
    }
    

通过此设置,您会发现相同的详细 View Controller 实例在选项卡之间共享,并且当您更改到新选项卡时,一个选项卡中的修改会保留。

关于ios - 多个 UISplitView 的一个详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990787/

相关文章:

ios - UISplitViewController - 在 vi​​ewDidLoad 上显示主视图的弹出窗口

multithreading - 在非主线程中使用不使用 CGContextRef 的 iOS CoreText API

iphone - 使用公司 iOS Developer 开发私有(private) iPhone

swift - 无法将 Swift 隐式解包可选声明为常量

ios - UITableView 显示循环的最后一个值

ios - 在 UISplitView 的 rootviewcontroller 中设置异常酷炫的选项卡栏

objective-c - 从一个模态视图无缝切换到另一个模态视图,不显示纯色背景

android - Cordova 应用程序在新应用程序版本中保留本地存储数据吗?

swift - 如何验证用户对游戏的输入而不必每次都按 'submit' 按钮

ios - UISplitViewController - 在折叠模式下关闭/弹出代码中的详细 View Controller