IBInspectable with Cocoa Touch Framework not working? (code attached)

时间:2016-11-12 06:04:54

标签: xcode8 ios10 ibdesignable ibinspectable cocoa-touch

I can not seem to get the "titleText" IBInspectable attribute working. I have a simple framework view with a UILabel which I create an IBInspectable variable "titleText". I note the inspectable "layer" variables work as expected, but not my custom "titleText" which is supposed to update the main view using this framework.

Issues are:

  1. Interface Builder not updating the titleLabel? (i.e. at design time) i.e. I'm expecting that this should, just like it is working for the "layer" items.

  2. At Run Time the value of titleLabel is not picking up the value I set in IB either. Note that I get the following output when I run, i.e. my code that produces this is finding the "self.titleLabel" is actually nil??

CODE SUMMARY

enter image description here

(a) gcCustomView.xib Snapshot enter image description here

(b) gcCustomerView.swift

import UIKit

@IBDesignable 
class gcCustomView: UIView {

    @IBOutlet weak var titleLabel: UILabel!
    @IBInspectable var titleText: String = "Default" {
        didSet {
            if  self.titleLabel != nil {
                self.titleLabel.text = titleText
            } else {
                NSLog("Could not set title text as label.text was nil : was trying to set to \(titleText)")
            }
        }
    }


    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        commitInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commitInit()
    }

    // Private

    func commitInit() {
        if self.subviews.count == 0 {
            print("Loading Nib")
            //let bundle = Bundle(forClass: self.dynamicType)
            let bundle = Bundle(for: type(of: self))
            let nib = UINib(nibName: "gcCustomView", bundle: bundle)
            let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
            view.frame = bounds
            view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            addSubview(view)
        }
    }

}

(c) Main.storyboard Snapshot

enter image description here

4 个答案:

答案 0 :(得分:4)

选择gcCustomView.xib File's Owner将班级名称更改为gcCustomerView

enter image description here

右键点击从Labeloutlet

File Owner拖动

enter image description here

现在,如果右键单击Label

,它应该是这样的

enter image description here

我做了一个演示项目。它工作正常。如果您还有问题,请告诉我。我将在Github上传我的演示项目。

答案 1 :(得分:2)

如果您想使用xib,请确保已在 fileowner

中设置了您的班级名称

先做那个

enter image description here

现在添加班级名称

enter image description here

添加 IBOutlet

参考Reuse a uiview xib in storyboard

答案 2 :(得分:2)

这是内存问题.Label这里没有内存。因此它返回0而不是在自定义类中加载nib,您应该在ViewController中加载它。 用以下代码替换你的gcCustomView。

<强> gcCustomView.swift

import UIKit

@IBDesignable
class gcCustomView: UIView {

    @IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        if  self.titleLabel != nil {
            self.titleLabel.text = "Default"
        }
        else {
            NSLog("Could not set title text as label.text was nil : was trying to set to default")
        }
    }
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }
    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
       // commitInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
       // commitInit()
    }
}

<强> ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.loadNib()

    }
    func loadNib() {
        let obj = Bundle.main.loadNibNamed("gcCustomView", owner: nil, options: nil)?[0] as! gcCustomView
        obj.frame = (self.view.bounds)
        self.view.addSubview(obj)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

然后建立。它会起作用。

答案 3 :(得分:0)

在自定义视图中删除Keypath参数并运行。它对我有用。 :)