MK_ MK_ - 2 months ago 11x
Swift Question

Fatal Error in QRCode Scanner App for IOS

I am working on an QRCode Scanner App for IOS with two Views. When I compile and launch the App on my iPad 2 and click my button on the toolbar I get an EXC_BREAKPOINT with subcode 0xe77ffdefe on both classes.

class ViewController: UIViewController {

@IBAction func showQRView(sender: AnyObject){

let secondView:QRCodeViewController = QRCodeViewController()
self.presentViewController(secondView, animated: true, completion: nil)

}//end of showQRView

override func viewDidLoad() {


override func didReceiveMemoryWarning() {


Second Class

class QRCodeViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {
@IBAction func closeView(sender:AnyObject)
self.dismissViewControllerAnimated(true, completion: nil)

@IBOutlet weak var messageLabel:UILabel!

var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?a

let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

override func viewDidLoad() {

messageLabel.frame = CGRectMake(0, 0, 768, 71)
messageLabel.backgroundColor = UIColor.blackColor()
messageLabel.textColor = UIColor(colorLiteralRed: 0.0, green: 255.0, blue: 0.0, alpha: 1.0)
messageLabel.textAlignment = NSTextAlignment.Center

let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

do {
let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession = AVCaptureSession()

let captureMetadataOutput = AVCaptureMetadataOutput()

captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())

captureMetadataOutput.metadataObjectTypes = supportedBarCodes

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds



qrCodeFrameView = UIView()

if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.greenColor().CGColor
qrCodeFrameView.layer.borderWidth = 2

} catch {


override func didReceiveMemoryWarning() {


func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRectZero
messageLabel.text = "No QR code is detected"

let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

if supportedBarCodes.contains(metadataObj.type) {

let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue



The issue is that calling QRCodeViewController() creates an instance of your QRCodeViewController that doesn't know anything about the views you've designed in interface builder. Instead, you need to tell the storyboard to instantiate an instance for you.

Open the storyboard and select your QRCodeViewController scene. Open the right hand pane (Utilities) and click the icon at the top for the identity inspector (looks like a small ID card.) Fill in the box for 'Storyboard ID' with a unique string.

Now, back in your code, replace the contents of your @IBAction func showQRView(sender: AnyObject) function with the following

let storyboard = UIStoryboard(name: "STORY_BOARD_NAME_HERE", bundle: nil)
let secondView = storyboard.instantiateViewControllerWithIdentifier("ID_YOU_SET_EARLIER_HERE") as! QRCodeViewController
self.presentViewController(secondView, animated: true, completion: nil)