子类的函数不会被调用 - SwiftUI

问题描述

我目前正在使用 SwiftUI,需要实现一个 UIViewController 类。从这个特定的类 (ViewController),我希望 Subclass 继承自它。 我现在的问题是,当我在超类中调用函数 setupAVCapture() 时,我认为在我调用 super.setupAVCapture() 时会先调用我子类中的 override 方法,然后调用超类中的 Method。 我错过了什么吗?

我首先做的是实现一个符合 UIViewControllerRepresentable 的结构,如下所示:

struct AVCaptureView: UIViewControllerRepresentable{
    func makeUIViewController(context: Context) -> CameraviewController {
        let cameraVC = CameraviewController()
        return cameraVC
    }
    func updateUIViewController(_ uiViewController: CameraviewController,context: Context) { }
}

这个类在我的 ContentView 主体中被调用

那么实际的超类实现如下:

class ViewController: UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate {
...
  override func viewDidLoad() {
        super.viewDidLoad()
        setupAVCapture()
  }
  func setupAVCapture {
    ...
  }
...
}

那么对应的子类是这样的:

class Subclass: ViewController {
   override func setupAVCapture() {
        super.setupAVCapture()
        ...
   }
}

这个方法 override func setupAVCapture 应该在 viewDidLoad 方法中从它的超类中调用。但它的作用是调用超类 func setupAVCapture()

中的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)