尝试在 SwiftUI 中使用来自 UIkit 的我的 UIViewController 会导致协议无法确认可表示的
Trying to use my UIViewController from UIkit in SwiftUI causes Protocol to not confirm to the representable
我正在学习并制作一个带有图形 API metal 的简单应用程序,我正在尝试通过 UIKit 在 SwiftUI 中显示我的视图控制器。我试图从我的 ViewController class 中创建 UIViewControllerRepresentable,但这一直导致类型 'UIViewController' 不符合协议 'UIViewControllerRepresentable' 的错误。这很可能是因为我的 ViewController 现在抛出一个 Inheritance from non-protocol, non-class type 'UIViewController' 所以它没有协议。那么如何使包含我所有金属代码的ViewController采用可表示需要的协议呢?
这是我的代码:
//ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
UIViewController()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//UIViewController.swift
import SwiftUI
import UIKit
struct UIViewController: UIViewControllerRepresentable { //error: Type 'UIViewController' does not conform to protocol 'UIViewControllerRepresentable'
func makeUIViewController(context: Context) -> ViewController{
return ViewController()
}
}
struct UIViewController_Previews: PreviewProvider {
static var previews: some View {
UIViewController()
}
}
//ViewController.swift
import UIKit
import Metal
class ViewController: UIViewController { //error: Inheritance from non-protocol, non-class type 'UIViewController'
var device: MTLDevice!
var metalLayer: CAMetalLayer!
//created is the buffer to send the points over to gpu
var vertexBuffer: MTLBuffer!
//starts the render pipeline
var pipelineState: MTLRenderPipelineState!
//command queue
var commandQueue: MTLCommandQueue!
//timer to render the screen frame by frame
var timer: CADisplayLink!
//insuring the view is loaded so the device can actually be used
override func viewDidLoad() { //error: Method does not override any method from its superclass
super.viewDidLoad()
device = MTLCreateSystemDefaultDevice()
metalLayer = CAMetalLayer()
metalLayer.device = device
metalLayer.pixelFormat = .bgra8Unorm
metalLayer.framebufferOnly = true
metalLayer.frame = view.layer.frame
view.layer.addSublayer(metalLayer) //error: Use of unresolved identifier 'view'
....
在我尝试在我的 SwiftUI 中显示它之前 ViewController 中没有错误。
很抱歉,我是 IOS 开发和图形方面的完全初学者,但感谢您的提前帮助。
对于错误
Type 'UIViewController' does not conform to protocol 'UIViewControllerRepresentable'
我猜你错过了UIViewControllerRepresentable
协议
中的方法updateUIViewController
此外,调用您的结构 UIViewController
可能 导致一些名称冲突
我正在学习并制作一个带有图形 API metal 的简单应用程序,我正在尝试通过 UIKit 在 SwiftUI 中显示我的视图控制器。我试图从我的 ViewController class 中创建 UIViewControllerRepresentable,但这一直导致类型 'UIViewController' 不符合协议 'UIViewControllerRepresentable' 的错误。这很可能是因为我的 ViewController 现在抛出一个 Inheritance from non-protocol, non-class type 'UIViewController' 所以它没有协议。那么如何使包含我所有金属代码的ViewController采用可表示需要的协议呢?
这是我的代码:
//ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
UIViewController()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//UIViewController.swift
import SwiftUI
import UIKit
struct UIViewController: UIViewControllerRepresentable { //error: Type 'UIViewController' does not conform to protocol 'UIViewControllerRepresentable'
func makeUIViewController(context: Context) -> ViewController{
return ViewController()
}
}
struct UIViewController_Previews: PreviewProvider {
static var previews: some View {
UIViewController()
}
}
//ViewController.swift
import UIKit
import Metal
class ViewController: UIViewController { //error: Inheritance from non-protocol, non-class type 'UIViewController'
var device: MTLDevice!
var metalLayer: CAMetalLayer!
//created is the buffer to send the points over to gpu
var vertexBuffer: MTLBuffer!
//starts the render pipeline
var pipelineState: MTLRenderPipelineState!
//command queue
var commandQueue: MTLCommandQueue!
//timer to render the screen frame by frame
var timer: CADisplayLink!
//insuring the view is loaded so the device can actually be used
override func viewDidLoad() { //error: Method does not override any method from its superclass
super.viewDidLoad()
device = MTLCreateSystemDefaultDevice()
metalLayer = CAMetalLayer()
metalLayer.device = device
metalLayer.pixelFormat = .bgra8Unorm
metalLayer.framebufferOnly = true
metalLayer.frame = view.layer.frame
view.layer.addSublayer(metalLayer) //error: Use of unresolved identifier 'view'
....
在我尝试在我的 SwiftUI 中显示它之前 ViewController 中没有错误。
很抱歉,我是 IOS 开发和图形方面的完全初学者,但感谢您的提前帮助。
对于错误
Type 'UIViewController' does not conform to protocol 'UIViewControllerRepresentable'
我猜你错过了UIViewControllerRepresentable
协议
此外,调用您的结构 UIViewController
可能 导致一些名称冲突