带有 Cocoa Touch Framework 的 IBInspectable 无法正常工作? (附代码)

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

我似乎无法使 "titleText" IBInspectable 属性正常工作。我有一个带有 UILabel 的简单框架视图,我创建了一个 IBInspectable 变量 "titleText"。我注意到可检查的 "layer" 变量按预期工作,但不是我的自定义 "titleText",它应该使用此框架更新主视图。

问题是:

  1. Interface Builder 没有更新 titleLabel? (即在设计时)即我期待这应该,就像它适用于 "layer" 项目一样。

  2. 在 运行 时,titleLabel 的值也未采用我在 IB 中设置的值。请注意,当我 运行 时,我得到以下输出,即我的代码发现 "self.titleLabel" 实际上是 nil??

代码摘要

(a) gcCustomView.xib 快照

(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 快照

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

中设置您的 class 名称

先做那个

现在添加class名字

并添加IBOutlet

参考

这是记忆issue.Label这里没有记忆。因此它 returns 0 而不是在自定义 class 中加载笔尖,你应该在 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.
    }
}

然后构建。它会起作用。

Select gcCustomView.xib File's Owner 将 class 名称更改为 gcCustomerView.

右键单击 Labeloutlet 拖动到 File Owner

现在右击应该是这样的Label

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

删除自定义视图中的 Keypath 参数,run.It 对我有用。 :)