Saswat Raj Pandey
Saswat Raj Pandey

Reputation: 75

Setting delegate for CustomUiView getting EXC_BAD_ACCESS

I have created a custom UIView and have a protocol set for it. Now from the View Controller when I set the delegate to self I am getting an EXC_BAD_ACCESS.

---The View Controller Code------

class VerificationController: UIViewController, LoadingViewDelegate {
   @IBOutlet weak var instructionView: LoadingView!

   override func viewDidLoad() {
       super.viewDidLoad()           
       instructionView.randomTextIndexes = [1]
       instructionView.delegate = self 
   }

   ...
}

// "instructionView" is the UIView outlet and "LoadingView" is the class

--This is the custom View Code ------

protocol LoadingViewDelegate {
    func generated(random code:String)
}

class LoadingView: UIView {  
    var delegate:LoadingViewDelegate?
    var randomTextIndexes:[Int] = []
}

I am getting EXC_BAD_ACCESS when I try to access the delegate as well as the randomTextIndexes from the viewDidLoad() method of the view controller. Could you please tell me what I am missing here.

Upvotes: 1

Views: 313

Answers (1)

Renzo Tissoni
Renzo Tissoni

Reputation: 759

My best guess is that you forgot to set the class of the custom view in the Interface Builder. Check the Identity Inspector for your object in the IB, it should look like this:

How it should look

If it isn't set, then that's the problem.

Upvotes: 7

Related Questions