Adz Adz - 11 months ago 49
iOS Question

Swift Error IBOutlet with xib file

In my project, I want to display a popup and the view of this popup is defined in a .xib file.
To do that I use this line of code :

myPopupView = NSBundle.mainBundle().loadNibNamed("NibView", owner: self, options: nil)[0] as! UIView

Until then, it works. But the problem is when I try to add an IBOutlet. My Xib file is connected to a uiviewcollection, and I manage to create an IBOutlet on the Textfield I have in the xib file. But when now I run again my app, I have this famous message :
this class is not key value coding-compliant for the key...

So if I look on Internet, everybody says to delete the IBOutlet by clicking on the cross button in Inspector, BUT I don't want to delete it because I need this IBOutlet. So isn't it possible to have IBOutlet in a xib file ?

Thanks :)

Answer Source

This is very common error which means the IBOutlet you have taken in .xib is not connected to the relevant class. This kind of error I experience when i have taken a outlet and given some name like lblUserName but later i renamed that to lblFriendName, then the label in xib is still connected to lblUserName as well as lblFriendName, so at the time of compilation the xib did not find the connection for lblUserName and throws error.

So go to your xib, right click on the outlet in the xib, then you will see the cross button to disconnect. So disconnect it and connect it again.

During that also you can see the connection name, it must be wrong name or there would be two connection foe the same label in which one would be correct and another must be wrong (This case when you rename a connection as i explained)

Hope you understand, and it helps...

Happy coding ...