0

我正在尝试在 swiftUi 项目中使用可表示的 UIViewController。具体来说,我试图按下一个按钮(assetOne),允许欧盟选择一个视频,然后按下另一个按钮(assetTwo),它允许用户选择另一个视频。然后用户可以选择合并视频(使用第三个按钮)。我认为我需要使用协调器来完成此操作,但是在看到没有它的SO 解决方案之后,我尝试在没有它的情况下执行此操作。但是当我运行我的项目时,构建是成功的,但是当我从内容视图中单击任何按钮时,我会收到下面的错误消息。我究竟做错了什么?我需要一个协调器吗?如何将它与我当前的配置结合起来?

警告:尝试在视图不在窗口层次结构中的 <TempTest.MergeVideoViewController: 0x7fa05ed088c0> 上呈现 <UIImagePickerController: 0x7fa05f827600>!

内容视图:

import SwiftUI

struct ContentView: View {
    
    let someView = ImagePicker()
    
    var body: some View {
        VStack {
            Button(action: {
                print("SwiftUI: assetOne button tapped")
                // Call func in SomeView()
                self.someView.assetOne()
            }) {
                Text("Asset One").foregroundColor(Color.black)
            }
            .background(Color.blue)
            .padding(10)
            .clipShape(Capsule())
}
//...

ImagePicker:UIViewControllerRepresentable

struct ImagePicker: UIViewControllerRepresentable{
    
    let someView = MergeVideoViewController()

func makeUIViewController(context: Context) -> MergeVideoViewController {
      someView
    }

    func updateUIViewController(_ uiViewController: MergeVideoViewController, context: Context) {}
    
    func assetOne() {
        someView.loadAssetOne()
    }
    //...
}

我的 UIViewController 类:

class MergeVideoViewController: UIViewController {
    var firstAsset: AVAsset?
    var secondAsset: AVAsset?
    var audioAsset: AVAsset?
    var loadingAssetOne = false
    var activityMonitor: UIActivityIndicatorView!
    
func exportDidFinish(_ session: AVAssetExportSession) {
    // Cleanup assets
    activityMonitor.stopAnimating()
    firstAsset = nil
    secondAsset = nil
    audioAsset = nil

//...

func loadAssetOne() {
        //  func loadAssetOne(_ sender: AnyObject) {
        if savedPhotosAvailable() {
            loadingAssetOne = true
            VideoHelper.startMediaBrowser(delegate: self, sourceType: .savedPhotosAlbum)
        }
    }

//...
4

1 回答 1

0

ImagePickeris-a 视图,它应该在body.

这是可能的方法 - 想法是在 SwiftUI 中获取控制器引用并在需要时直接调用其操作。

struct ImagePicker: UIViewControllerRepresentable{
    let configure: (MergeVideoViewController) -> ()

    func makeUIViewController(context: Context) -> MergeVideoViewController {
       let someView = MergeVideoViewController()
       configure(someView)
       return someView
    }

    func updateUIViewController(_ uiViewController: MergeVideoViewController, context: Context) {}
}

struct ContentView: View {

    @State private var controller: MergeVideoViewController?

    var body: some View {
        VStack {
            ImagePicker {
                self.controller = $0
            }

            Button(action: {
                print("SwiftUI: assetOne button tapped")
                self.controller?.loadAssetOne()
            }) {
                Text("Asset One").foregroundColor(Color.black)
            }
            .background(Color.blue)
            .padding(10)
            .clipShape(Capsule())
        }
    }
}

于 2020-09-10T03:22:02.823 回答