gpt4 book ai didi

iOS Swift - 在容器 View 中实现 SplitViewController

转载 作者:行者123 更新时间:2023-11-30 11:39:20 24 4
gpt4 key购买 nike

我正在关注本教程 link

但是我从一开始就做了一个改变。

我添加了一个 ViewController(并将其设为初始 View Controller )并在其中添加了一个容器 View 。

然后我在我的 View 容器和教程的 SplitViewController 之间嵌入 seagued。

一切都很顺利,直到我到达这一步:

Go to to AppDelegate.swift and replace the implementation of application(_:didFinishLaunchingWithOptions:) with the following:

guard let splitViewController = window?.rootViewController as? UISplitViewController,
let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
let masterViewController = leftNavController.topViewController as? MasterViewController,
let detailViewController = splitViewController.viewControllers.last as? DetailViewController
else { fatalError() }

let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster

return true

应如何修改此代码以适合我的情况?

最佳答案

在您的情况下,您无法访问 AppDelegate 中的 uisplitviewcontroller。由于您使用的是容器嵌入 View ,如下图所示。您可以从第一个 viewController 的prepareFor segue 获取 UISplitviewcontroller 的引用。因此,不要在 AppDelegate 中编写代码,而是尝试在初始 viewController 中为 segue 做准备。

enter image description here enter image description here

初始 View Controller ViewController 类:UIViewController {

            @IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

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

if segue.identifier == "embedseg" {
guard let splitViewController = segue.destination as? UISplitViewController,
let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
let masterViewController = leftNavController.topViewController as? MasterViewController,
let rightNavController = splitViewController.viewControllers.last as? UINavigationController,
let detailViewController = rightNavController.topViewController as? DetailViewController
else { fatalError() }

let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster

masterViewController.delegate = detailViewController

detailViewController.navigationItem.leftItemsSupplementBackButton = true
detailViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
}

}
}

enter image description here

关于iOS Swift - 在容器 View 中实现 SplitViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com