2016-08-31 80 views
-3

我有下面的代碼和它應該做的是這樣的: 當相機檢測到QR碼時,它應該通過標識符SendDataSegue打開我的DetailViewController。問題是,當檢測到QR碼時,什麼都沒有發生?有人能幫我一下嗎?這是代碼的底部,我正在嘗試使用prepareforsegue。用qr代碼激活prepareforsegue

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) { 
 
     
 
     // Check if the metadataObjects array is not nil and it contains at least one object. 
 
     if metadataObjects == nil || metadataObjects.count == 0 { 
 
      qrCodeFrameView?.frame = CGRectZero 
 
      messageLabel.text = "No barcode/QR code is detected" 
 
      return 
 
     } 
 
     
 
     // Get the metadata object. 
 
     let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject 
 
     
 
     // Here we use filter method to check if the type of metadataObj is supported 
 
     // Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type 
 
     // can be found in the array of supported bar codes. 
 
     if supportedBarCodes.contains(metadataObj.type) { 
 
//  if metadataObj.type == AVMetadataObjectTypeQRCode { 
 
      // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds 
 
      let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj) 
 
      qrCodeFrameView?.frame = barCodeObject!.bounds 
 
      
 
      if metadataObj.stringValue != nil { 
 
       
 
       
 
       func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
 
        if (segue.identifier == "SendDataSegue") { 
 
         // pass data to next view 
 
        } 
 
       } 
 
       
 
       
 
       
 
       
 
      } 
 
     } 
 
    }

+0

您不要在那裏聲明該方法。你只需要'self.performSegue(「SendDataSegue」,sender:nil)''。 – Larme

+0

但是如果我有一個標籤,在我的DetailViewController上應該更改爲QR代碼所說的內容? –

+0

當你使用你的代碼@Larme它說:類型ViewController的值沒有成員preformSegue –

回答

0

這段代碼幫助我:

dispatch_async(dispatch_get_main_queue()){ 
       self.performSegueWithIdentifier("SendDataSegue", sender:self) 


      } 

有誰知道如何通過傳遞數據?我可以通過一個正常的func prepareforsegue傳遞數據,但是我怎麼通過這個來做到這一點?