本文介绍了IBInspectable与Cocoa Touch Framework不兼容吗? (附加代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题是:

  1. Interface Builder是否未更新titleLabel? (即在设计时),即我希望这应该适用于图层"项目.

  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.

在运行时,titleLabel的值也未获取我在IB中设置的值.请注意,我在运行时会得到以下输出,即我生成此代码的代码是发现"self.titleLabel"实际上为nil ??

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??

代码摘要

(a)gcCustomView.xib快照

(a) gcCustomView.xib Snapshot

(b)gcCustomerView.swift

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

(c) Main.storyboard Snapshot

推荐答案

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

Select gcCustomView.xib File's Owner change class name to gcCustomerView.

右键单击Label,将其从outlet拖动到File Owner

Right click on the Label drag from outlet to File Owner

现在,如果右键单击Label

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

I made a demo project. It is working ok. If you still have problem let me know. I'll upload my demo project in Github.

这篇关于IBInspectable与Cocoa Touch Framework不兼容吗? (附加代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:10