我的应用程序中有一个扫描ISBN代码的视图控制器,这是类:
isbn
}
它扫描确切的代码并将其保存在var isbn中。错误是当我执行segue时,因为它多次返回到前一个视图控制器而我只想要一个。我试图打印变量CREATE SEQUENCE SEQ_USR
MINVALUE 1 MAXVALUE 1000
START WITH 1
INCREMENT BY 1;
并多次打印,如下所示:“
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666
9788804648666"
答案 0 :(得分:0)
您的问题来自于检测到QR码时仍在运行的 captureSession ,因此多次调用 performSegue 。 相反,你应该替换
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
QRScannerController.isbn = metadataObj.stringValue
print(QRScannerController.isbn)
self.performSegue(withIdentifier: "showISBN", sender: self)
}
通过
if let code = metadataObj.stringValue {
saveAndDismiss(code: code)
}
并添加
fileprivate func saveAndDismiss(code code: String) {
messageLabel.text = code
QRScannerController.isbn = code
print(QRScannerController.isbn)
performSegue(withIdentifier: "showISBN", sender: self)
captureSession?.stopRunning()
}
此外,你的isbn var不应该是静态的。它应该是私有的,您可以在méthode prepareForSegue
中将值赋给segue目标控制器答案 1 :(得分:0)
我甚至会替换整个功能:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
guard let data = metadataObjects,
data.count > 0 else {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
// Get the metadata object.
if let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObject,
supportedCodeTypes.contains(metadataObj.type) {
// If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
if let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) {
qrCodeFrameView?.frame = barCodeObject.bounds
}
if let code = metadataObj.stringValue {
saveAndDismiss(code: code)
}
}
}