'object.stringValue from QR code is not being parsed to a Variable in a different class

I have the code to scan QR code from my app but I want the string that is obtained from the QR code: object.stringValue to be passed into a variable of my choice.

I searched a lot about this specific topic on log-ing in with QR code or how to parse data from the object.stringValue to variables but nothing came up.

This function below is in class QRCodeViewController

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    if metadataObjects != nil && metadataObjects.count != 0
    {
        if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
        {
            if object.type == AVMetadataObject.ObjectType.qr
            {
                let alert = UIAlertController()
                alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                alert.addAction(UIAlertAction(title: "Vazhdo", style: .default, handler: {_ in
                    
                    let theID = object.stringValue
                    
                    let viewController = MonitorimiViewController()
                    viewController.id = theID!
                
                    let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
                    controller?.modalTransitionStyle = .flipHorizontal
                    controller?.modalPresentationStyle = .fullScreen
                    self.present(controller!, animated: true, completion: nil)
      
                }))
                    
                    present(alert, animated: true, completion: nil)
            }
        }
    }
}

I want that object.stringValue to be parsed here on the MonitorimiViewController which is the other class

var id: String = ""


Solution 1:[1]

You are setting the string to MonitorimiViewController but you are opening tabBarController.

let viewController = MonitorimiViewController()
viewController.id = theID!
                
let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
controller?.modalTransitionStyle = .flipHorizontal
controller?.modalPresentationStyle = .fullScreen
self.present(controller!, animated: true, completion: nil)

Try to set the string value to tabBarController.

let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
controller?.id = theID!
controller?.modalTransitionStyle = .flipHorizontal
controller?.modalPresentationStyle = .fullScreen
self.present(controller!, animated: true, completion: nil)

After this, you need to set the id value in the tab bar controller when you set the view controller.

In MonitorimiViewController you can assign the value as

let viewController = MonitorimiViewController()
viewController.id = id

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darshan