Reputation: 1160
I'm trying to make a custom view that can be used in the storyboard. Thing is it crashes when the view is instantiated. I don't understand the error or where I could pin point the the fault. Here are a couple images of my issues.
If anybody could please help me. That would be much appreciated. If you need more details please tell me where to find the details that you need and I can fetch them for you.
Upvotes: 0
Views: 413
Reputation: 310
There's another great way to init custom view programmatically. You need to make the following:
1. Write extension for your UIView class
extension UIView {
class func initFromNib<T: UIView>() -> T {
return Bundle.main.loadNibNamed(String(describing: self), owner: nil, options: nil)?[0] as! T
}
}
2. Create .swift and .xib files with the same name as your Class' name:
3. Inside your FMTextInputView.swift file you should init your nib:
private var param1: String!
private var param2: String!
//MARK: - init
static func instantiate(param1: String, param2: String) -> FMTextInputView {
let view: FMTextInputView = initFromNib()
view.param1 = param1
view.param2 = param2
return view
}
4. Now you can use your custom view easily wherever you want:
let inputView = FMTextInputView.instantiate(param1: "Hello", param2: "world")
self.view.addSubview(inputView)
Upvotes: 1