Swift - Custom View with xib file, IBOutlet is nil

5.3k views Asked by At

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!

3

There are 3 answers

0
Hoang Trung On BEST ANSWER

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

0
zouritre On

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

1
Sagar Chauhan On

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.