我想通过单击按钮进入CameraViewController,然后拍摄qr代码。 我一直收到这个错误 感谢大家的帮助 我在Storyboard中为每个ViewController提供了它的类型
import UIKit
class movieListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var movieTableView: UITableView!
var movie:[Movie] = [Movie]()
var currentMovies:[Movie] = [Movie]()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
movie = Helpers.downloadJson()
self.movieTableView.reloadData()
sort()
}
func sort() {
movie.sort(by: { $0.releaseYear < $1.releaseYear })
movieTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return movie.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! MovieCell
cell.createCell(movie: movie[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMovie = movie[indexPath.row]
performSegue(withIdentifier: "goToDetail", sender: selectedMovie)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nextVC = segue.destination as! DetailViewController
nextVC.movie = sender as! Movie
}
@IBAction func addTapped(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextView = storyBoard.instantiateViewController(withIdentifier: "goToC") as! CameraViewController
self.present(nextView, animated: true, completion: nil)
}
}
CameraViewController:
import UIKit
import AVFoundation
class CameraViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
} catch {
print(error)
return
}
let captureMetaDataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetaDataOutput)
captureMetaDataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetaDataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
qrCodeFrameView = UIView()
if let newFrame = qrCodeFrameView {
newFrame.layer.borderColor = UIColor.green.cgColor
newFrame.layer.borderWidth = 2
view.addSubview(newFrame)
view.bringSubview(toFront: newFrame)
}
}
}
DetailViewController
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var movieImage: UIImageView!
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var releaseYearLbl: UILabel!
@IBOutlet weak var ratingLbl: UILabel!
var movie: Movie = Movie()
override func viewDidLoad() {
super.viewDidLoad()
titleLbl.text = movie.title
ratingLbl.text = "Rating: \(movie.rating) ⭐️"
releaseYearLbl.text = "Release Year: \(movie.releaseYear)"
movieImage.layer.cornerRadius = 5.0
movieImage.clipsToBounds = true
movieImage.sd_setImage(with: URL(string: movie.image))
}
@IBAction func backToList(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}