因此,對于我所在的這個專案,我需要使用 QR 碼實作登錄,但我是 swift 新手,我不知道如何
現在我正在實作掃描并獲取顯示字串的 QR 碼,我希望將該字串放入 API ID 變數中,該變數從 API 呼叫獲取我的情況下的汽車串列
這是 QRCodeViewController() 類,一個從掃描中獲取字串的函式:
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
if metadataObjects != nil && metadataObjects.count != 0
{
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
{
if object.type == AVMetadataObject.ObjectType.qr
{
let alert = UIAlertController()
alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Vazhdo", style: .default, handler: {_ in
let idfromQR = object.stringValue
let viewController = MonitorimiViewController()
viewController.id = idfromQR!
let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
controller?.modalTransitionStyle = .flipHorizontal
controller?.modalPresentationStyle = .fullScreen
self.present(controller!, animated: true, completion: nil)
}))
present(alert, animated: true, completion: nil)
}
}
}
}
現在我想要那個物件。來自掃描的 QR 的 StringValue 被傳遞到 MonitorimiViewController() 中,MonitorimiViewController() 是另一個保存需要從物件獲取的 ID 值的類。StringValue 但我正在努力傳遞該值,因為它沒有被決議,我嘗試了所有我知道的但都未能被決議。
這是 MonitorimiViewController() 類所以 object.stringValue 必須在此傳遞var id: String = "object.stringValue"
var id: String = "0E79C6205FD04F8994B13F5255B7FB05"
uj5u.com熱心網友回復:
問題出在以下代碼行中:
let idfromQR = object.stringValue
let viewController = MonitorimiViewController()
viewController.id = idfromQR!
let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController")
controller?.modalTransitionStyle = .flipHorizontal
controller?.modalPresentationStyle = .fullScreen
self.present(controller!, animated: true, completion: nil)
您正在將 qr 代碼回應分配給永遠不會出現的視圖控制器。因此,您應該將其分配給標簽欄控制器的視圖控制器之一。
用此代碼替換上面的代碼以解決您的問題:
guard let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController else {
// Something wrong with your identifier
return
}
(tabBarController.viewControllers[0] as? MonitorimiViewController)?.id = idfromQR!
tabBarController.modalTransitionStyle = .flipHorizontal
tabBarController.modalPresentationStyle = .fullScreen
self.present(tabBarController, animated: true, completion: nil)
這假設 MonitorimiViewController 是標簽欄控制器中的第一個索引視圖控制器,如果它不只是將 0 索引更改為另一個數字。
uj5u.com熱心網友回復:
您正在創建一個新的 viewController,將 QR 碼值傳遞給它,然后您對 viewController 什么都不做,它就會記憶體不足。
let idfromQR = object.stringValue
let viewController = MonitorimiViewController()
viewController.id = idfromQR
viewController在這一點之后你什么都不做。您從情節提要創建一個 tabBarController,它會創建這些類的新副本并打開這些新副本。
你需要做這樣的事情:
guard let controller = self.storyboard?.instantiateViewController(withIdentifier: "tabBarController") as? UITabBarController,
let monitorController = controller.viewControllers.first as? MonitorimiViewController else {
return
}
monitorController.id = idfromQR
self.present(controller, animated: true, completion: nil)
我在這里做的是:
- 從情節提要中獲取 tabBarController
- 獲取它管理的視圖控制器之一(我正在搶第一個,我不知道你的索引是什么)。
- 檢查它們是否都可以轉換為正確的型別。
- 將值傳遞
MonitorimiViewController給 tabBar 正在使用的物件 - 顯示帶有更新后的 tabBar
MonitorimiViewController
還請確保使用if let xxxorguard let xxx陳述句并idfromQR!不惜一切代價避免強制展開(例如)。如果出現問題,它將使您的應用程式崩潰
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421125.html
標籤:
