【问题标题】:IBInspectable with Cocoa Touch Framework not working? (code attached)带有 Cocoa Touch 框架的 IBInspectable 不起作用? (附代码)
【发布时间】:2017-03-26 09:31:59
【问题描述】:

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

问题是:

  1. 界面生成器不更新 titleLabel? (即在设计时)即我期望这应该,就像它适用于“层”项目一样。

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

【问题讨论】:

    标签: xcode8 ios10 ibdesignable ibinspectable cocoa-touch


    【解决方案1】:

    选择gcCustomView.xib File's Owner 将类名更改为gcCustomerView

    右击Labeloutlet拖到File Owner

    现在右击Label应该是这个样子

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

    【讨论】:

    • 非常感谢 - 这个答案与我出错的地方保持一致,并记录了如何纠正问题
    【解决方案2】:

    如果你想使用 xib,请确保你已经在 fileowner 中设置了你的类名

    首先要做到这一点

    现在添加类名

    并添加IBOutlet

    参考Reuse a uiview xib in storyboard

    【讨论】:

    • 我添加了文件所有者,但这没有帮助...我会查看您发送的链接
    【解决方案3】:

    这是内存问题。标签在这里没有内存。因此它返回 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.
        }
    }
    

    然后构建。它会起作用的。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2015-02-05
        • 2015-03-18
        • 2018-02-02
        • 2014-12-03
        • 2014-11-12
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 2017-06-01
        相关资源
        最近更新 更多