如何清除Label.text中的信息?

时间:2016-09-02 22:05:12

标签: swift uilabel

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

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


   }

问题是,当我单击取消按钮并返回上一个viewcontroller时,当我重新打开qr代码扫描程序时,我看到最后扫描的代码,显示在Label.text中。您能否帮我清除标签并不显示旧代码,因为这些代码只能使用一次,如果用户看到代码,他们就可以再次使用它们。

2 个答案:

答案 0 :(得分:0)

如果您不想添加其他回调:

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

否则当你从SecondSecondViewController回来并设置Label.text = ""

时,你必须抓住unwind segue

答案 1 :(得分:0)

只需将其添加到退出QR的位置(按取消):

label.text = ""