-1

I am programmatically creating a UIView that is being loaded from a nib in a separate class in its own init method:

@IBOutlet weak var label: UILabel! 

override init (frame: CGRect) {
     super.init(frame: frame)
     let xibView = UINib(nibName: "aView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
     self.addSubview(xibView)
}

In another class I am calling my UIView class and trying to set text to my label:

self.aView = AView()
self.aView?.frame = CGRect(x: 0, y: 0, width: 
self.aView?.label.text = "Sample Text"
self.view.bounds.size.width, height: 200)
self.pageScrollView.addSubview(self.aView!)

Whenever I attempt to set "Sample Text" to my UILabel I keep getting the error:

Optional unexpectedly found nil while attempting to unwrap

So since this is being loaded from its nib is it because the view has yet to be drawn to the view, so it wouldn't be able to set the text to label property? If so, how can I fix this?

butter_baby
  • 858
  • 2
  • 10
  • 24
  • You are missunderstanding two things: Nibs are not created programatically, their format is `.xib`. `UIView` created programatically doesn't have any connections with storyboards. – yerpy Mar 27 '18 at 06:55
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Mar 27 '18 at 06:57
  • > `in a separate class in its own init method:` what is the "separate class"? For example, it is the view controller that loaded from storyboard or just simple class? – pacification Mar 27 '18 at 07:08
  • show how do you declare `self.aView` property – Milan Nosáľ Mar 27 '18 at 07:14

3 Answers3

0

In most cases this message appear (relatively to IBOutlet)

Optional unexpectedly found nil while attempting to unwrap

when you didn't connect IBOutlet in interface builder. Try to do that. Should work.

pacification
  • 5,838
  • 4
  • 29
  • 51
  • This `UILabel` is not part of the `aView` as we can see. Nib file doesn't use initializer with `frame`. – yerpy Mar 27 '18 at 06:57
  • @yerpy, yes, you right, but we know nothing about label holder class. if it is `UIViewController`, author should connect `UILabel` in storyboard, for example. If not - this way is not working, obviously. – pacification Mar 27 '18 at 07:11
  • 1
    Yeah, added code is not showing problem, it's only presenting missunderstanding of creating custom views. It's messed up. – yerpy Mar 27 '18 at 07:15
0

It does not seem your aView is not retained. So I'm afraid ARC has released it for you.

I think you should add a strong reference to your AView somewhere, maybe it's because you have not added it to its superview.

Or maybe your label has not been linked to your XIB file.

Zaphod
  • 6,758
  • 3
  • 40
  • 60
0

thtyh

You can not access IBOutlets, just like you access the other variables only by creating an object of AView()!!!!!

Enea Dume
  • 3,014
  • 3
  • 21
  • 36