gpt4 book ai didi

swift - 我可以从 Storyboard 中实例化一个 Swift 自定义 UIView 而无需嵌套吗?

转载 作者:行者123 更新时间:2023-11-28 08:04:44 27 4
gpt4 key购买 nike

在我寻找如何使用 nib 制作自定义 UI View 的每个地方,我都会看到以下要使用的代码

class CustomView: UIView {


var contentView: UIView!;

@IBOutlet weak var sampleLabel: UILabel!

init() {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
if self.subviews.count == 0 {
nibSetup()
}

}

override init(frame: CGRect) {
super.init(frame: frame)
if self.subviews.count == 0 {
nibSetup()
}
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
nibSetup()
}

}


fileprivate func nibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.translatesAutoresizingMaskIntoConstraints = true
addSubview(contentView)
}

fileprivate func loadViewFromNib() -> TimerView {

let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView

return nibView
}
}

但这基本上是将自定义 View 的新实例(从 Nib 加载)加载到原始实例的 contentView 中。这一直有效,直到你想从它所在的 View Controller 调用你的 View 上的方法。当你想从 View Controller 调用实例上的方法时,你是在原始实例而不是 cotnentView 中的实例上调用它,所以结果是什么也没发生。

作为解决方法,我已将 contentView 声明为 CustomView 而不是 UIView,然后我的公共(public)方法调用内容 View 上的方法

class CustomView: UIView {


var contentView: CustomView!;

@IBOutlet weak var sampleLabel: UILabel!

init() {
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
if self.subviews.count == 0 {
nibSetup()
}

}

override init(frame: CGRect) {
super.init(frame: frame)
if self.subviews.count == 0 {
nibSetup()
}
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
nibSetup()
}

}


fileprivate func nibSetup() {
contentView = loadViewFromNib()
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.translatesAutoresizingMaskIntoConstraints = true
addSubview(contentView)
}

fileprivate func loadViewFromNib() -> TimerView {

let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! CustomView

return nibView
}

func setLabelText(blah: String) {
sampleLabel.text = blah
}

public func setLabelTextFromParent(words: String) {
contentView.setLabelText(blah: words)
}
}

虽然这看起来真的很老套。必须有更好的方法来做到这一点!

有人可以向我解释如何执行此操作,以便在从 IB 实例化自定义 View 时,我只有一个自定义 View 实例,而不是一个嵌套在另一个实例中。谢谢。

最佳答案

你可以在 UIView 上定义一个扩展,比如

extension UIView {
class func fromNib<T : UIView>() -> T {
return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
}
}

我在这里没有看到任何黑客攻击。像这样调用它

let myCustomView: MyCustomView = UIView.fromNib()

关于swift - 我可以从 Storyboard 中实例化一个 Swift 自定义 UIView 而无需嵌套吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45501438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com