EXC_BAD_ACCESS when performing a segue to ViewController with a UIWebView?

1.5k Views Asked by At

So I have a setup like this:

enter image description here

Where I have a custom code reader view controller, and as soon as it detects a barcode, I want it to segue to another view controller with a label and a UIWebView. So far, it works perfectly with just the label being displayed. For some reason, all I did was add a UIWebView and added an IBOutlet in my Second View Controller custom class and the segue won't work due to the exc_bad_access error. The error highlights the following line in my code reader view controller:

self.performSegueWithIdentifier("push", sender: self) //my segue is named push

Here is my second view controller code:

import UIKit

public class SecondViewController: UIViewController {
    var setText:String!
    @IBOutlet weak var WebV: UIWebView!


    @IBOutlet var label: UILabel!
    override public func viewDidLoad() {
        label.text = setText
    }

}

SOLUTION: So I just realized that, for some reason, my segue was running off the main thread. Does this happen in a closure? And, does this mean that you can't perform a segue in a closure/block?

Any help is greatly appreciated. Thank you so much.

3

There are 3 best solutions below

0
On BEST ANSWER

So I just realized that, for some reason, my segue was not running on the main thread. Dispatching to the main queue fixed the issue.

5
On

Assuming from your code,

you have no code to pass the String from CodeReaderViewController to SecondViewController.

Here is what I did(actually for a barcode reader that I did):

class CodeReaderViewController : UIViewController {
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "push" {
            let subVC: SecondViewController = segue.destinationViewController as SecondViewController
            subVC.setText = <ANY TEXT WHICH YOU'D LIKE TO PASS TO SECONDVIEWCONTROLLER>
        }
    }
}

If it doesn't work, please tell me, as this was written right after Swift came out.

0
On

If you are already calling prepareForSegue in the main thread but still getting bad access, double check if you are using the correct view controller class in the storyboard.