我有 3 个具有不同主视图的 UISplitViewController
,但它们具有相同的详细 View 。所有这些都在 Storyboard 中连接。
所有 UISplitViewController
都嵌套在 UITabBarViewController
中,所以我通过标签栏项在它们之间切换。
问题是,当我切换到另一个选项卡(另一个 UISplitViewController
)时,详细 View 消失了,我只看到主视图,详细 View 的位置充满了深灰色(见图)。我不想在切换后重新加载详细 View ,只需将其保留在屏幕右侧即可。
我不确定我需要提供什么代码,所以如果你需要任何代码,请询问,我会把它添加到问题中。
感谢您的帮助!
最佳答案
原因
我的第一个假设是,如果您在两个不同的 UISplitViewController
之间共享一个详细 View Controller ,这对应于 UITabController
的两个选项卡,两个单独的详细 View Controller 被创建。
具有此布局的测试项目证实了这一点:
Root View Controller
是一个 DetailViewController
。当我在 viewDidLoad(_:)
中放置一个断点时,它被命中两次并且打印显示创建了两个不同的 DetailViewController
实例:
(lldb) po self
<TestTabSplit.DetailTableViewController: 0x7fbd10eb9cd0>
(lldb) po self
<TestTabSplit.DetailTableViewController: 0x7fbd10ebc700>
解决方案
使用共享容器 View Controller 作为两个 UISplitViewController
的详细 View Controller 。
您的新 Storyboard布局将如下所示:
- 为您的详细 View Controller (在本例中为导航 Controller )提供 Storyboard ID:
接下来,在您的应用委托(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 }
最后,容器 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/