问题描述
我正在努力在我现有的应用程序中实现一些SwiftUI内容.我目前有一个UIViewController,该UIViewController托管一个MTKView来进行相机预览.
I am working to implement some SwiftUI content into my existing app. I currently have a UIViewController, which hosts a MTKView for camera preview.
我已经创建了一个新的SwiftUI视图,该视图现在是我的 root 视图,与我的SceneDelegate.swift
文件中的设置相同.如预期的那样,SwiftUI视图将在启动时加载.现在,我想创建一个segue,当用户点击列表中的一行时,它将全屏显示到我现有的UIViewController中.这就是我的称呼方式;
I have created a new SwiftUI view, which is now my root view, as set in my SceneDelegate.swift
file. The SwiftUI view loads at launch, as expected. Now, I would like to create a segue in which, when a user taps on a row in my List, it will segue, full-screen to my existing UIViewController. Here is how I'm calling that;
var body: some View {
VStack {
NavigationView {
List(sessionTypes) { session in
NavigationLink(destination: CameraControllerWrapper()) {
SessionRow(session: session)
.frame(height: 40.0)
}
}
.navigationBarTitle(Text("Camera Types"))
}
}
}
为后代,这是我的CameraControllerWrapper
UIViewControllerRepresentable;
For posterity, here is my CameraControllerWrapper
UIViewControllerRepresentable;
struct CameraControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = CameraController
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraControllerWrapper>) -> CameraControllerWrapper.UIViewControllerType {
return CameraController()
}
func updateUIViewController(_ uiViewController: CameraControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<CameraControllerWrapper>) {
//
}
}
尽管这可行",但我的应用程序在调用CameraController时立即崩溃,因为似乎找不到我的任何IBOutlets. CameraController是内置在情节提要中的UIViewController.
While this "works," my app crashes as soon as the CameraController is called, as it seems any of my IBOutlets cannot be found. CameraController is a UIViewController built in the storyboard.
推荐答案
我设法解决了这一问题,因为我意识到需要从Storyboard而不是代码中实例化UIViewController(因为我已经在Storyboard中构建了它的布局,一些程序化元素).要使用上面的NavigationLink
,我需要这样调整UIViewControllerRepresentable
;
I managed to resolve this by realizing that I needed to instantiate the UIViewController from the Storyboard, and not in code (as I had built its layout in Storyboard, alongside some programatic elements). To use the above NavigationLink
, I needed to adjust my UIViewControllerRepresentable
as such;
struct CameraControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = CameraController
func makeUIViewController(context: UIViewControllerRepresentableContext<CameraControllerWrapper>) -> CameraControllerWrapper.UIViewControllerType {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: CameraController = mainStoryboard.instantiateViewController(withIdentifier: "CameraController") as! CameraController
return mainViewController
}
func updateUIViewController(_ uiViewController: CameraControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<CameraControllerWrapper>) {
//
}
}
这篇关于从SwiftUI视图中选择到UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!