从UIViewController中的SwiftUIView分配@state var

时间:2019-12-03 17:44:24

标签: ios swift uikit swiftui

我想将我的SwiftUIView中的@State变量放在UIViewController中。 我的UIViewController是扫描器/阅读器的二维码,当它检测到QRCode时,我希望状态变量自动更新,并将状态变量分配给找到的URL。我不知道问题出在哪里,这是代码:

ContentView.swift:

import SwiftUI
struct ContentView: View {

    @State var url = "url"

    var body: some View {
                VStack {
                    SCRepresentable(test: $url)
                    Text(test)
                        .font(.title)
                        .foregroundColor(.white)
                }.padding(.bottom, 2)
        }
    }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ScannerViewController.swift(在我尝试self.url = stringValue(检测到qr代码时找到的url)的函数中查找METADATOUTPUT):

import AVFoundation
import UIKit
import SwiftUI

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!
    var url: String = "url"


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.black
        captureSession = AVCaptureSession()

        guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
        let videoInput: AVCaptureDeviceInput

        do {
            videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
        } catch {
            return
        }

        if (captureSession.canAddInput(videoInput)) {
            captureSession.addInput(videoInput)
        } else {
            failed()
            return
        }

        let metadataOutput = AVCaptureMetadataOutput()

        if (captureSession.canAddOutput(metadataOutput)) {
            captureSession.addOutput(metadataOutput)

            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            metadataOutput.metadataObjectTypes = [.qr]
        } else {
            failed()
            return
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)

        captureSession.startRunning()
    }


    func failed() {
        let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
        captureSession = nil
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if (captureSession?.isRunning == false) {
            captureSession.startRunning()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if (captureSession?.isRunning == true) {
            captureSession.stopRunning()
        }
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        captureSession.stopRunning()

        if let metadataObject = metadataObjects.first {
            guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
            guard let stringValue = readableObject.stringValue else { return }
            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
           self.url = stringValue
            found(code: stringValue)
        }

        dismiss(animated: true)
    }

    func found(code: String) {
        print(code)
        print("url: ", self.url)
    }

    func rrr() -> String{
        return self.url
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

SCRepresentable.swift:

import SwiftUI

struct SCRepresentable: UIViewControllerRepresentable {

    @Binding var test: String


    typealias UIViewControllerType = ScannerViewController

    func makeUIViewController(context: UIViewControllerRepresentableContext<SCRepresentable>) -> ScannerViewController {
        let scan = ScannerViewController()
        test = scan.url
        return scan
    }

    func updateUIViewController(_ uiViewController: ScannerViewController, context: UIViewControllerRepresentableContext<SCRepresentable>) {
        //Add anything
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您好,我看到您想在QR码检测到您不需要注释@State时进行处理,您需要将viewController与@Binding绑定,让我在代码中进行更多说明:

ScannerViewController中,我们需要声明需要在@Binding var urlDetected: String中初始化的init(),我们将此函数称为:

init(urlDetected: Binding<String>) {
        _urlDetected = urlDetected
        super.init(nibName: nil, bundle: nil) 
    }

需要在SCRepresentable中传递另一个绑定变量  @Binding var urlDetected:字符串

func makeUIViewController(context: UIViewControllerRepresentableContext<SCRepresentable>) -> ScannerViewController {
        let scan = ScannerViewController(isDetected: $urlDetected)
        return scan
    }

因此,当您在

中声明它时,现在var urlDetected中有这个SCRepresentable
struct ContentView: View {

    @State var url = "url"

    var body: some View {
                VStack {
                    SCRepresentable(urlDetected: $url)
....<#Code#>

当viewController中的URL更改时,它将通知并更新视图

但是不要忘了像这样self. urlDetected = .constant(stringValue

在viewController内部设置一个值