如何使初始视图控制器再次出现在iOS中?

时间:2018-07-30 16:24:30

标签: ios swift uiviewcontroller storyboard

我有一个使用主屏幕(在下面)的应用程序,然后使用条形码扫描器框架扫描条形码,然后对要执行的操作进行编码。但是,我遇到的问题是在代码中,它带我回到扫描屏幕。我尝试使用present(VC1, animated: true, completion: nil),但它不知道VC1是什么。

我在情节提要ID下(见下文)在Xcode中分配了VC1:

Storyboard ID for VC1

下面是我在这里用于视图controller.swift的代码。现在,代码将您带回到扫描屏幕,但是我想回到主视图控制器VC1。

import UIKit
import BarcodeScanner

var presentedViewController: UIViewController?

final class ViewController: UIViewController {
    @IBOutlet var pushScannerButton: UIButton!

    //Present view and handle barcode scanning
    @IBAction func handleScannerPush(_ sender: Any, forEvent event: UIEvent) {
        let viewController = makeBarcodeScannerViewController()
        viewController.title = "Barcode Scanner"
        present(viewController, animated: true, completion: nil)
    }

    private func makeBarcodeScannerViewController() -> BarcodeScannerViewController {
        let viewController = BarcodeScannerViewController()
        viewController.codeDelegate = self
        viewController.errorDelegate = self
        viewController.dismissalDelegate = self
        return viewController
    }
}

// MARK: - BarcodeScannerCodeDelegate

extension ViewController: BarcodeScannerCodeDelegate {
    func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
        print("Barcode Data: \(code)")
        print("Symbology Type: \(type)")

        controller.dismiss(animated: true, completion: nil)

//       DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
//           controller.resetWithError()
//        }
    }
}

// MARK: - BarcodeScannerErrorDelegate

extension ViewController: BarcodeScannerErrorDelegate {
    func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
        print(error)
    }
}

// MARK: - BarcodeScannerDismissalDelegate

extension ViewController: BarcodeScannerDismissalDelegate {
    func scannerDidDismiss(_ controller: BarcodeScannerViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
}

0 个答案:

没有答案