Hoang Trung
Hoang Trung

Reputation: 263

Swift - Custom View with xib file, IBOutlet is nil

I have a custom button designed using xib file like this: enter image description here

and I connect it to my storyboard like this:

enter image description here

but when I run the app, I got runtime error complaining that IBOutlet ivRightIcon is nil

@IBInspectable var rightIcon: UIImage? = nil{
    didSet{
            ivRightIcon.image = rightIcon
        }
    }
}

My outlet is connected in xib file correctly I'm using Swift 3 and xCode 8.2.1 Please help!

Upvotes: 3

Views: 5489

Answers (3)

zouritre
zouritre

Reputation: 655

I solved that error by modifying the outlets values in viewWillAppear instead of viewDidLoad. This way the outlets is fully loaded when accessed.

Upvotes: 0

Hoang Trung
Hoang Trung

Reputation: 263

I managed to solve this. I should set the View class as UIButton and File's Owner as my custom button class

Upvotes: 4

Sagar Chauhan
Sagar Chauhan

Reputation: 5823

I think you need to connect your xib with file responder and assign class to file responder instead of view. then connect IBOutlet of xib to the class.

Upvotes: 1

Related Questions