简体   繁体   中英

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". 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:

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".

Am I even on the right path here? My thought is maybe my Util class doesn't have access to Bundle or something?

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

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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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