I have been trying to figure this out for days and haven't had much luck :(
我一直试图弄清楚这几天,并没有太多的运气:(
What I want to do is set the variable inside of an instance of a XIB (called BottomNav) that already exists in another ViewController, called "curX". I have come the closest with the following:
我想要做的是将变量设置在另一个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
}
}
Here is the BottomNav Class:
这是BottomNav类:
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 passes the compiler with no warnings, but when it's ran I get a "this class is not key value coding-compliant for the key" error. This usually appears when there's an outlet that no longer exists, but this is definitely not the case, I've tested it with multiple XIBs that are already on the app, and had no problem loading in the first place via storyboards. I only get this error with "loadNibNamed".
这会在没有警告的情况下通过编译器,但是当它运行时,我得到一个“这个类不是密钥值编码兼容的密钥”错误。这通常出现在有一个不再存在的插座时,但绝对不是这种情况,我已经使用已经在应用程序上的多个XIB进行了测试,并且首先通过故事板加载没有问题。我只用“loadNibNamed”得到这个错误。
Am I even on the right path here? My thought is maybe my Util class doesn't have access to Bundle or something?
我甚至走在正确的道路上吗?我的想法可能是我的Util类无法访问Bundle或其他东西?
2 个解决方案
#1
4
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)
}
}
And when calling the instance, try the following.
在调用实例时,请尝试以下操作。
let instance = BottomNav()
instance.curX = 30
#2
2
Swift 3.0
I Think you get you solution from this. All the best
我想你能从中得到解决方案。祝一切顺利
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)
}
#1
4
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)
}
}
And when calling the instance, try the following.
在调用实例时,请尝试以下操作。
let instance = BottomNav()
instance.curX = 30
#2
2
Swift 3.0
I Think you get you solution from this. All the best
我想你能从中得到解决方案。祝一切顺利
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)
}