简体   繁体   English

Swift 3-从另一个类的XIB实例访问变量

[英]Swift 3 - Access Variable From Instance Of XIB In Another Class

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". 我想做的是在XIB(称为BottomNav)实例中设置变量,该实例已存在于另一个名为“ curX”的ViewController中。 I have come the closest with the following: 我最接近以下几点:

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. 通常在没有出口的情况下会出现这种情况,但绝对不是这种情况,我已经使用应用程序中已有的多个XIB对其进行了测试,并且首先通过情节提要加载时没有问题。 I only get this error with "loadNibNamed". 我仅通过“ 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之类的东西?

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

Swift 3.0 斯威夫特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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM