I am trying to subclass a UIView in Swift.
Howver the app crashes (EXC_BAD_ACCESS) when the initializer is called
Here is the class
class CustomActionSheet: UIView {
private var cancelButtonTitle: String!;
private var destructiveButtonTitle: String!;
private var otherButtonTitles: [String]!;
convenience init() {
self.init();//EXC_BAD_ACCESS
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
}
convenience init(cancelButtonTitle: String!, destructiveButtonTitle: String!, otherButtonTitles: [String]!) {
self.init();
self.cancelButtonTitle = cancelButtonTitle;
self.destructiveButtonTitle = destructiveButtonTitle;
self.otherButtonTitles = otherButtonTitles;
prepareUI();
}
func prepareUI() {
//BLABLABLABLABLA
}
}
Here is how I call it
var actionSheet: CustomActionSheet = CustomActionSheet(cancelButtonTitle: "Cancel", destructiveButtonTitle: "OK", otherButtonTitles: nil);
Tried to replace self.init() with super.init() but it won't compile.
Error Message:
Must call a designated initializer of the superclass 'UIView'
Convenience initializer for 'CustomActionSheet' must delegate (with 'self.init') rather than chaining to a superclass initializer (with 'super.init')
You need to initializer your UIVIew with a frame, even if it is zero, you also need to override the init so you can initialize your variables before call supper:
Notice I remove the other convenience initializer you create given all your variables are private there is no need to this initializer, however if you want to have an empty initializer you can just add as:
Also the size of the frame can be passed or fix you just need to make the appropriate changes and call
init(frame: yourFrame)
Rules for initialization from Apple Documentation:
I hope that helps you!