如何准备segue并将数据从1个标签发送到另一个?

时间:2016-09-02 20:27:05

标签: swift uilabel segue

我正在开发QR码阅读器应用程序。这是代码:

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


  }

当我编写函数prepareForSegue时,我尝试将Label.text(扫描结果)中的数据发送到下一个label1中的UIViewController。很遗憾,Label.text的颜色为红色(无法将类型String的值指定为UILabel类型)。

你知道我该如何管理它? (我只想将扫描的信息发送给其他UIViewController

4 个答案:

答案 0 :(得分:3)

改变:

PC.label1.text = Label.text!

您正在尝试将字符串分配给类型为UILabel的变量,而不是其文本值。

答案 1 :(得分:1)

试试这个:

在目的地VC - SecondViewController中添加此变量:

var labelText : String = ""

在同一个类中将此行添加到viewDidLoad()方法中:

label1.text = labelText

然后您需要访问该字符串变量并将值放入其中。

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

答案 2 :(得分:1)

PC.label1无法加载数据集PC.label1.text

试试这种方式

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

答案 3 :(得分:1)

添加PC.label1.text = Label.text!