Dakata Dakata - 3 months ago 7
Swift Question

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

I am developing a QR code reader application. Hier 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 last function prepare for segue, I am trying to send the data from Label.text (the result from the scan) to label1 in my next viewcontroller. 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 viewcontroller.

Answer

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!
}
Comments