设置CustomUiView的委托以获取EXC_BAD_ACCESS

时间:2019-06-18 19:20:08

标签: ios swift uiview delegates

我创建了一个自定义UIView并为其设置了协议。现在,从View Controller中,当我将委托设置为self时,我得到一个EXC_BAD_ACCESS。

--- View Controller代码------

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

-这是自定义的查看代码------

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

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

当我尝试从视图控制器的EXC_BAD_ACCESS方法访问委托以及randomTextIndexes时,得到viewDidLoad()。你能告诉我我在这里想念的吗?

1 个答案:

答案 0 :(得分:2)

我的最佳猜测是您忘记了在Interface Builder中设置自定义视图的类。在IB中检查您的对象的Identity Inspector,它应如下所示:

How it should look

如果未设置,那就是问题。