@IBInspectable coming back nil in custom button class

时间:2017-07-30 11:44:33

标签: ios swift xcode interface-builder

I followed this tutorial in order to create reusable UI components for interface builder. I have followed the article exactly and the results I'm getting are the same as explained in the article.

The issue I'm not having is that I have added an @IBInspectable property called style to the class, which shows up fine in the properties inspector of interface builder, however no matter what I do it always comes back nil in both the Setup() and loadViewFromNib() methods of the MyButton class.

Being new to Interface Builder custom components, I am starting to wonder whether the method followed in the article breaks @IBInspectable properties, as I have used them successfully past by using custom UIButton classes without adding the extra XIB component.

Any help would be much appreciated.

    @IBDesignable
    class MyButton: UIButton {
        @IBOutlet weak var view: UIButton!
        @IBInspectable var style: String!
...

1 个答案:

答案 0 :(得分:0)

您永远不会使用默认值初始化变量。

尝试将其设置为等于空字符串并删除显式解包,如下所示:

@IBInspectable var style: String = ""