Gre*_*reg 6 cocoa-touch ibdesignable ibinspectable ios10 xcode8
我似乎无法获得"titleText"IBInspectable属性.我有一个带有UILabel的简单框架视图,我创建了一个IBInspectable变量"titleText".我注意到可检查的"层"变量按预期工作,但不是我的自定义"titleText",它应该使用此框架更新主视图.
问题是:
Interface Builder没有更新titleLabel?(即在设计时),即我希望这应该,就像它适用于"层"项目.
在运行时,titleLabel的值也没有获取我在IB中设置的值.请注意,当我运行时,我得到以下输出,即产生这个的代码是发现"self.titleLabel"实际上是零?
代码摘要
(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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
(c)Main.storyboard Snapshot
选择gcCustomView.xib File's Owner将类名称更改为gcCustomerView。
右键单击Label从 拖动outlet到File Owner
现在如果你右键单击它应该看起来像这样Label
我做了一个演示项目。工作正常。如果您仍然有问题,请告诉我。我将在 Github 上上传我的演示项目。
| 归档时间: |
|
| 查看次数: |
693 次 |
| 最近记录: |