Ios 尝试将变量从一个类移动到另一个类
我目前正在尝试将一个变量从一个类移动到另一个类。我正在使用Ios 尝试将变量从一个类移动到另一个类,ios,swift,avfoundation,qr-code,Ios,Swift,Avfoundation,Qr Code,我目前正在尝试将一个变量从一个类移动到另一个类。我正在使用AVFramework读取二维码。QR码最终读取字符串变量,然后字符串变量读取label.text。我想在另一个类的textview中使用完全相同的文本 QRScannerController包含QR代码,而HistoryView是我想重复使用变量的地方。问题是,一旦我扫描二维码并移动到历史视图,它什么也看不到。这是我到目前为止所拥有的。下面是QRScannerController func prepareForSegue(segue:
AVFramework
读取二维码。QR码最终读取字符串变量,然后字符串变量读取label.text。我想在另一个类的textview中使用完全相同的文本
QRScannerController
包含QR代码,而HistoryView
是我想重复使用变量的地方。问题是,一旦我扫描二维码并移动到历史视图,它什么也看不到。这是我到目前为止所拥有的。下面是QRScannerController
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.destination .isKind(of:HistoryView.self){
let vc2 = segue.destination as! HistoryView
vc2.previousViewController = self
}
}
@IBOutlet var messageLabel:UILabel!
@IBOutlet var topbar: UIView!
@IBOutlet var segmentedControl: UISegmentedControl!
//@IBOutlet var textFacts: UITextView!
var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?
let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode]
@IBAction func unwindToHomeScreen(segue: UIStoryboardSegue) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input)
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
// Start video capture.
captureSession?.startRunning()
// Move the message label and top bar to the front
view.bringSubview(toFront: messageLabel)
view.bringSubview(toFront: topbar)
// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubview(toFront: qrCodeFrameView)
}
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods
public func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from 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 = CGRect.zero
//messageLabel.text = "No QR/barcode is 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 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 {
//captureSession?.stopRunning()
messageLabel.text = metadataObj.stringValue
messageLabel.text = messageLabel.text
//let vc: UINavigationController = storyboard?.instantiateViewController(withIdentifier: "View") as! UINavigationController
let vc3: UIViewController = storyboard!.instantiateViewController(withIdentifier: "View") //as! UIViewController
self.present(vc3, animated: true, completion: nil)
}
}
}
func transferViewControllerVariables() -> (UILabel){
return messageLabel
}
}
以下是历史视图:
import UIKit
import AVFoundation
class HistoryView: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
@IBOutlet var textHistory:UITextView!
var previousViewController: QRScannerController?
@IBAction func unwindToHomeScreen(segue: UIStoryboardSegue) {
dismiss(animated: true, completion: nil)
}
@IBAction func toMaps(segue: UIStoryboardSegue) {
let vc3: UIViewController = storyboard!.instantiateViewController(withIdentifier: "front") //as! UIViewController
self.present(vc3, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let messageLabel = previousViewController?.transferViewControllerVariables()
//print(messageLabel.text as Any)
textHistory.text = messageLabel?.text
}
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您应该在
HistoryView
中声明messageLabel
。执行segue时,分配值,就这样
class HistoryView: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var messageLabel: String?
}
class QRCodeScanner: UIViewController {
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.destination.isKind(of:HistoryView.self){
if let vc2 = segue.destination as? HistoryView {
vc2.messageLabel = self.messageLabel.text
}
}
}
}
我建议您将实际文本传递给下一个视图控制器,而不是整个上一个视图控制器。 但一般的问题是永远不会调用prepareForSegue函数,因为HistoryView控制器不是通过segue显示的,而是通过编程方式显示的 要以编程方式将数据传递给HistoryView控制器,请在QRScannerViewController的captureOutput函数中执行此操作:
let vc3: HistoryView = storyboard!.instantiateViewController(withIdentifier: "View") as! HistoryView
vc3.messagetext = metadataObj.stringValue
self.present(vc3, animated: true, completion: nil)
要在历史记录中添加新属性,请执行以下操作:
class HistoryView: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var messageText: String?
}
然后可以完全删除prepareForSegue函数,因为它没有被调用。为什么不使用segue,而是在
QRScannerController
中使用present
和vc3
?只需将其更改为performsgue
,然后它就可以工作了。这可能会有帮助。首先,QRCodeScanner中的第二条if语句无效,并导致错误。其次,我得到错误“QRScannerController类型的值没有成员messageLabel”,它应该是您的@IBOutlet var messageLabel:UILabel代码>是!!我已经这样做并让它执行了,但是文本显示为空。您是否将textHistoryText分配给messageText,如下所示:textHistory.text=messageText?当您到达prepareForReuse时,是否设置了messageLabel上的文本?是的,我已指定了文本。我最后有准备功能。这是在切换到新视图之后,因此此时应该设置消息标签。它仍然什么也看不见。这使我确信数据不会传输到另一个类。但是您确实在prepareForSegue中到达了这一行:vc2.messageText=self.messageLabel.text?我如何知道是否确实到达了这一行?