UIView subclass in Swift: IBOutlet null detected during unpacking

I am trying to create a custom view in Swift by subclassing UIView , and I have a view pane named MyViewPanel.xib that has a class assigned by MyCustomView . The implementation is as follows:

 import UIKit @IBDesignable class MyCustomView: UIView { @IBOutlet weak var title: UILabel! var question: Question { didSet { print("did set question, title is: \(question.title)") } } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func drawRect(rect: CGRect) { let height = rect.height let width = rect.width let color: UIColor = UIColor.whiteColor() let drect = CGRect(x: (width * 0.25), y: (height * 0.25), width: (width * 0.5),height: (height * 0.5)) let bpath: UIBezierPath = UIBezierPath(rect: drect) color.set() bpath.stroke() } override func awakeFromNib() { super.awakeFromNib() print("awake from nib!") self.title.text = "Test title" // error: found nil while unwrapping an Optional Value } } 

During runtime, I found the following error:

 fatal error: unexpectedly found nil while unwrapping an Optional value 

Since awakeFromNib() is the first lifecycle event in UIView , I don’t understand why in this case the UILabel title is zero.

Edit:

To use this custom view, I simply draw a UIView rectangle on my storyboard and assign it to the MyCustomView class. In the viewDidLoad() method of my ViewController for the storyboard, I asked a question on a user view:

 override func viewDidLoad() { // myCustomView is an IBOutlet in the view controller myCustomView.question = question } 
+5
source share
3 answers

Part of the code looks fine, but this may mean that the sockets are not properly connected in the interface builder. In the "Outputs" section of the interface builder, you can see that the title bar is connected and an exclamation mark is not displayed, indicating an error. Also, the IBOutlet line in the code should have a filled circle next to it to indicate that the socket is connected correctly and the class is correctly assigned from the interface constructor.

+1
source

I figured out how to set text for UILabel in a convenient initializer for subclassing UIView. I wrote:

 @IBOutlet weak var messageLabel: UILabel! init?(frame: CGRect, message: String) { super.init(frame: frame) guard let view = NSBundle.mainBundle().loadNibNamed("MyViewSubclass", owner: self, options: nil).first as? MyViewSubclass else { return nil } // Without the following line, my view was always 600 x 600 view.frame = frame self.addSubview(view) guard let messageLabel = view.messageLabel else { return } messageLabel.text = message } 
+1
source

It happened to me, thus creating my own look

 MyCustomView() 

instead of creating an instance from the storyboard

 UIStoryboard.init(name: "name", bundle: nil).instantiateViewController(withIdentifier: "identifier") 
0
source

Source: https://habr.com/ru/post/1240252/


All Articles