如何在扫描QR码后转到新的viewcontroller?

时间:2017-01-12 15:40:41

标签: ios swift uiviewcontroller swift3

我现在试了几个小时试图让我的应用程序运行起来。问题是,在应用检测到QR码后,它不会显示新的视图控制器。它将在该视图的新视图控制器中执行简单的打印语句,但应用程序中不存在新的视图控制器。我在Stackoverflow上已经尝试了很多解决方案,但似乎没有一个在这个特定情况下有所帮助。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    captureSession.stopRunning()

    if let metadataObject = metadataObjects.first {
        let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;

        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        found(code: readableObject.stringValue);

    }
    let vc = infoViewController()
    self.present(vc, animated: true, completion: nil)

我使用last to lines指向应用程序扫描QR码后应该去的新infoViewController。 infoViewController中的代码如下:

import UIKit

class infoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        print("Hi")
}

Log确实显示了打印语句“Hi”,但应用程序没有转到infoViewController。故事板中还包含infoViewController,并选择了infoViewController类。

我目前缺少一个细节吗?

问候

1 个答案:

答案 0 :(得分:0)

首先,您必须选择infoViewController上的storyboard,在身份检查器中,您必须添加Storyboard ID,如下所示:

enter image description here

然后你必须像这样展示infoViewController

OperationQueue.main.addOperation {
    let infoViewController = self.storyboard?.instantiateViewController(withIdentifier: "infoViewController"
    self.present(infoViewController, animated: true)
}