【问题标题】:UIAlertController move to another controller on alert action - SwiftUIAlertController 在警报操作时移动到另一个控制器 - Swift
【发布时间】:2018-08-28 08:30:55
【问题描述】:

我正在尝试在警报视图操作中移动到另一个控制器。它正在按预期移动到下一个控制器,但是一旦它移动到另一个控制器,它就会再次显示相同的警报。请检查以下代码。

func decode(decodedURL: String) {
    let alertController = UIAlertController(title: "", message: "Confirm", preferredStyle: .alert)

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FormViewController") as? FormViewController
    viewController?.fillData(dataDic: convertJsonStringToDictionary(jsonString: decodedURL))

    let action1 = UIAlertAction(title: "Ok", style: .default) { (action) in       
        self.navigationController?.show(viewController!, sender: true)
    }


    let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
        print("You've pressed cancel");       
    }

    alertController.addAction(action1)
    alertController.addAction(action2)

    self.present(alertController, animated: true, completion: nil)

}

在下面调用上面的方法

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    // Check if the metadataObjects array is not nil and it contains at least one object.
    if metadataObjects.count == 0 {
        qrCodeFrameView?.frame = CGRect.zero
        messageLabel.text = "No code detected"
        return
    }
    // Get the metadata object.
    let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
    if supportedCodeTypes.contains(metadataObj.type) {
        // If the found metadata is equal to the QR code metadata (or barcode) then update the status label's text and set the bounds
        let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
        qrCodeFrameView?.frame = barCodeObject!.bounds
        if metadataObj.stringValue != nil {
            decode(decodedURL: metadataObj.stringValue!)
            messageLabel.text = metadataObj.stringValue

        }
    }
}

【问题讨论】:

  • 我有一个带有确定和取消按钮的警报视图。
  • 这段代码在当前viewController的哪个函数中执行?
  • @Mac_Play,如果我没记错的话。 metadataOutput 函数可以被多次调用。直到你调用了 captureSession?.stopRunning() captureSession = nil
  • metadataOutput 是 AVCaptureMetadataOutputObjectsDelegate 的委托函数。它会继续被调用,直到你停止它。
  • Yes , write like it, if (//required scan complete) { captureSession?.stopRunning() captureSession = nil } In side metaDataOutPut

标签: ios swift uiviewcontroller uialertcontroller uialertaction


【解决方案1】:
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

的委托函数

AVCaptureMetadataOutputObjectsDelegate

。它会继续被调用,直到您的 captureSession 还活着。

因此,当您导航到下一个屏幕时,只需停止 captureSession。

captureSession?.stopRunning() 
captureSession = nil

【讨论】:

  • 无法将“AVMetadataFaceObject”(0x1b245bd28)类型的值转换为“AVMetadataMachineReadableCodeObject”(0x1b245be68)。经常出现这个错误,你知道吗?
猜你喜欢
  • 1970-01-01
  • 2016-04-28
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多