我遵循本教程link
但是我从一开始就做了一个改变。
我添加了一个ViewController(并使其成为初始视图控制器)并在其中添加了一个容器视图。
然后我在我的View容器和教程的SplitViewController之间嵌入了。
一切顺利,直到我完成这一步:
转到AppDelegate.swift并用以下内容替换application(_:didFinishLaunchingWithOptions :)的实现:
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
如何修改此代码以使用我的案例?
答案 0 :(得分:0)
在您的情况下,您无法访问AppDelegate中的uisplitviewcontroller。由于您正在使用Container Embedded视图,如下图所示。您可以从第一个viewController的prepareFor segue获得UISplitviewcontroller的参考。因此,不要在AppDelegate中编写代码,而是尝试在初始viewController中准备segue。
初始ViewController class 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
}
}
}