Swift 3 - 从另一个 Class 中的 XIB 实例访问变量
Swift 3 - Access Variable From Instance Of XIB In Another Class
几天来我一直在努力解决这个问题,但运气不佳:(
我想要做的是在另一个 ViewController 中已经存在的 XIB(称为 BottomNav)实例中设置变量,称为 "curX"。我最接近以下:
class Util: NSObject {
class func loadNib() {
let nib: BottomNav = Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)!.first as! BottomNav
nib.curX = 10
}
}
这是 BottomNav Class:
class BottomNav: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var homeBtn: UIView!
@IBOutlet weak var scroller: UIScrollView!
@IBOutlet weak var scrollerContent: UIView!
var curX = 32
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)
self.addSubview(self.view)
}
}
这通过了编译器,没有任何警告,但是当它是 运行 时,我得到一个 "this class is not key value coding-compliant for the key" 错误。这通常在有一个不再存在的出口时出现,但绝对不是这种情况,我已经使用应用程序上已有的多个 XIB 对其进行了测试,并且首先通过故事板加载没有问题。我只在 "loadNibNamed".
时收到此错误
我走的路对吗?我的想法是,也许我的 Util class 无法访问 Bundle 或其他东西?
Swift 3.0
我认为您可以从中得到解决方案。祝一切顺利
var view: UIView!
override init(frame: CGRect) {
// call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
// call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
xibSetup()
}
// MARK: - UI setup
func xibSetup() {
let nib = UINib(nibName: "BottomNav", bundle: nil)
view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
}
class BottomNav: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var homeBtn: UIView!
@IBOutlet weak var scroller: UIScrollView!
@IBOutlet weak var scrollerContent: UIView!
var curX = 32
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)
guard let content = view else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}
}
并且在调用实例时,尝试以下操作。
let instance = BottomNav()
instance.curX = 30
几天来我一直在努力解决这个问题,但运气不佳:(
我想要做的是在另一个 ViewController 中已经存在的 XIB(称为 BottomNav)实例中设置变量,称为 "curX"。我最接近以下:
class Util: NSObject {
class func loadNib() {
let nib: BottomNav = Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)!.first as! BottomNav
nib.curX = 10
}
}
这是 BottomNav Class:
class BottomNav: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var homeBtn: UIView!
@IBOutlet weak var scroller: UIScrollView!
@IBOutlet weak var scrollerContent: UIView!
var curX = 32
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)
self.addSubview(self.view)
}
}
这通过了编译器,没有任何警告,但是当它是 运行 时,我得到一个 "this class is not key value coding-compliant for the key" 错误。这通常在有一个不再存在的出口时出现,但绝对不是这种情况,我已经使用应用程序上已有的多个 XIB 对其进行了测试,并且首先通过故事板加载没有问题。我只在 "loadNibNamed".
时收到此错误我走的路对吗?我的想法是,也许我的 Util class 无法访问 Bundle 或其他东西?
Swift 3.0
我认为您可以从中得到解决方案。祝一切顺利
var view: UIView!
override init(frame: CGRect) {
// call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
// call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
xibSetup()
}
// MARK: - UI setup
func xibSetup() {
let nib = UINib(nibName: "BottomNav", bundle: nil)
view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
}
class BottomNav: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var homeBtn: UIView!
@IBOutlet weak var scroller: UIScrollView!
@IBOutlet weak var scrollerContent: UIView!
var curX = 32
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
Bundle.main.loadNibNamed("BottomNav", owner: self, options: nil)
guard let content = view else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}
}
并且在调用实例时,尝试以下操作。
let instance = BottomNav()
instance.curX = 30