Dakata
Dakata

Reputation: 1355

How to prepare for segue and send data from 1 label to another?

I am developing a QR code reader application. Here is the code:

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeframeView: UIView?

@IBOutlet weak var CancelButton: UIButton!
@IBOutlet weak var Label: UILabel!
override func viewDidLoad() {

    CancelButton.hidden = true
    Label.hidden = true


    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func ScanMe(sender: AnyObject) {

    CancelButton.hidden = false
    Label.hidden = false


    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    var error: NSError?
    let input: AnyObject!

    do {
        input = try AVCaptureDeviceInput (device: captureDevice)
    } catch let error1 as NSError{

        error = error1
        input = nil

    }

    if (error != nil){
    print ("\(error?.localizedDescription)")
        return
    }
    captureSession = AVCaptureSession()

    captureSession?.addInput(input as! AVCaptureInput)

    let captureMetadatOutput = AVCaptureMetadataOutput()
    captureSession?.addOutput(captureMetadatOutput)

    captureMetadatOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
    captureMetadatOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer!)

    captureSession?.startRunning()
    view.bringSubviewToFront(Label)
    view.bringSubviewToFront(CancelButton)

    qrCodeframeView = UIView()
    qrCodeframeView?.layer.borderColor = UIColor.greenColor().CGColor
    qrCodeframeView?.layer.borderWidth = 2
    view.addSubview(qrCodeframeView!)
    view.bringSubviewToFront(qrCodeframeView!)


}

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

    if metadataObjects == nil || metadataObjects.count == 0 {

        qrCodeframeView?.frame = CGRectZero
        Label.text = "No QR code detected"
        return
    }
    let metadateObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

    if metadateObj.type == AVMetadataObjectTypeQRCode {

        let BarcodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadateObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
        qrCodeframeView?.frame = BarcodeObject.bounds

        if metadateObj.stringValue != nil {

            Label.text = metadateObj.stringValue

            captureSession?.stopRunning()



 }
  }
 }


@IBAction func Cancel(sender: AnyObject) {
    CancelButton.hidden = true
    Label.hidden = true


    captureSession?.stopRunning()
    qrCodeframeView?.removeFromSuperview()
    videoPreviewLayer?.removeFromSuperlayer()


}

@IBAction func Open(sender: AnyObject) {

}
   override func prepareForSegue(segue: UIStoryboardSegue, sender:   AnyObject?) {
        var PC : SecondSecondViewController = segue.destinationViewController as! SecondSecondViewController
   PC.label1 = Label.text!
  }


  }

When I write the function prepareForSegue, I am trying to send the data from Label.text (the result from the scan) to label1 in my next UIViewController. Unfortunately, Label.text is colored in red (cannot assign value of type String to type UILabel).

Do you have any idea how can I manage that? (I want only to send the scanned information to the other UIViewController)

Upvotes: 3

Views: 319

Answers (4)

Ben
Ben

Reputation: 86

add PC.label1.text = Label.text!

Upvotes: 1

Nazmul Hasan
Nazmul Hasan

Reputation: 10590

PC.label1 unable to load data set PC.label1.text

try this way

override func prepareForSegue(segue: UIStoryboardSegue, sender:   AnyObject?) {
            var PC : SecondSecondViewController = segue.destinationViewController as! SecondSecondViewController
       PC.label1.text = Label.text!
      }

Upvotes: 1

pedrouan
pedrouan

Reputation: 12910

Try this:

In your destination VC - SecondViewController add this variable:

var labelText : String = ""

in the same class add this line into viewDidLoad() method:

label1.text = labelText

Then you need to access that string variable and put the value in it.

override func prepareForSegue(segue: UIStoryboardSegue, sender:   AnyObject?) {
        var PC : SecondSecondViewController = segue.destinationViewController as! SecondSecondViewController
        PC.labelText = Label.text!
}

Upvotes: 1

Marco Santarossa
Marco Santarossa

Reputation: 4066

Change in this:

PC.label1.text = Label.text!

You are trying to assign a String to a variable of type UILabel instead of its text value.

Upvotes: 3

Related Questions