1

I have a few questions regarding nibs.

  1. How do I load a nib onto my UIView in a View Controller dynamically or programmatically? I know you can just add the custom class of the nib by entering the nib's swift class here:

Custom Class

What I'm looking for is how to change that attribute, for example on a button press I would like to remove it or add it. I tried instantiating the swift file but I can't pull it off. I also tried changing the owner of the .xib file but it only wants a UIView so I can't make my View Controller its owner. I tried using

view.addSubiew(view: UIView)

But I don't know how to instantiate my Nib file properly as a UIView. It keeps asking for an NSCoder and I have no clue how to instantiate one either. It would help if someone could tell me how to add and remove the custom class that would be helpful.

  1. How do I pass data from my View Controller to the nib?

  2. How do I run functions once the nib has fully loaded? Here's the code of the .xib file's swift class.

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)    
        if (self.subviews.count == 0) {
            guard let view = loadViewFromNib() else {return}
            view.frame = self.bounds
            self.addSubview(view)
            mainView = view
            -> btn.layer.cornerRadius = 5
        }
    }
    
    private func loadViewFromNib() -> UIView? {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "NibFile", bundle: bundle)
        return nib.instantiate(withOwner: self, options: nil).first as? UIView
    }
    

This loads the view just fine but whenever I try to run anything else in the code blocks, like where it's marked (I wanted to change the corner radius of a button once the view is loaded), I keep getting a nil IBOutlet. It just keeps saying that the button is nil. Where do I add this function?

I also tried doing this:

override func awakeFromNib() {
    btnLogin.layer.cornerRadius = 5
}

Still nil, I'm getting confused here and research comes to dead ends or just what I don't need. Any help will be appreciated. Thank you!

Chris
  • 29
  • 9
  • 1-) This blog can be a good starting point for creating custom view. https://medium.com/@brianclouser/swift-3-creating-a-custom-view-from-a-xib-ecdfe5b3a960 2-) One option for passing data can be with any method you've created in your custom view. 3-) For awakeFromNib you can check this out: https://stackoverflow.com/a/31993253/4484349 – alitosuner Nov 06 '18 at 13:51
  • Will check these out. Thank you @alitosuner! – Chris Nov 07 '18 at 03:25

1 Answers1

0

So I did a lot of testing and came up with these:

  1. How do I load a nib onto my UIView in a View Controller dynamically or programmatically?

I removed the required init? function from the nib and just initialize it in the view controller. Basically I moved the code that loads the nib file from the required init? function to wherever I want to load it, say a button press event at some view controller. I just add this:

func loadView(nibName: String, uiview: UIView) {
    let view: UIView = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?[0] as! UIView
    view.frame = uiview.bounds
    view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    uiview.addSubview(view)
}

uiview is the view where I want to add the nib to.

  1. How do I pass data from my View Controller to the nib?

I call a function in the nib and pass the data as the parameter/s.

  1. How do I run functions once the nib has fully loaded?

If I do what I did in the first question then I can just run any function in the

override func awakeFromNib() {
}

and it runs once the nib is loaded.

Chris
  • 29
  • 9