Search code examples
swiftxibnibiboutlet

Unable to connect IBOutlet to XIB File


I am trying to implement Koloda card swiping style. I have created a .xib file structured as Image 1 & 2.xib file structure

As you can see in Image 1, I have set the File's Owner as the XIB Files custom class.

In image 2, I have left the class blank (not sure if this is correct)

My NIB class is per the below code.

import UIKit
import Koloda

class CardView: KolodaView {

var view: UIView!
var nibName: String = "CardView"
var uid: String!

@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var nameLabel: UILabel!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setUp()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    setUp()
}

func setUp() {

    view = loadViewFromNib()

    view.frame = bounds
    view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.isUserInteractionEnabled = true
    addSubview(view)

}

func loadViewFromNib() -> UIView {
    let bundle = Bundle(for: CardView.self)
    let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
}
}

In my controller, I have the view for index as per the below;

func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {

    _ = userResults[Int(index)]

    let bundle = Bundle(for: CardView.self)
    let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
}

I am unable to insert my IBOutlets and reference them in my UIViewController. As I will be getting geoFire & FirDatabase info and populating the views. I understand this has something to do with the File's Owner. I'm just unsure on how to set the IBOutlets to the subview instead of the custom class.

Any help appreciated.


Solution

  • Finally after hours of tinkering around.

    1. Class is now;

      import UIKit
      
      class CardView: UIView {
      
      var uid: String!
      
      @IBOutlet weak var profileImage: UIImageView!
      @IBOutlet weak var nameLabel: UILabel!
      
      }
      
    2. Removed file owner

    3. CardView in xib is now set to CardView
    4. viewForCardAt is now;

      func koloda(_ koloda: KolodaView, viewForCardAt index: Int) -> UIView {
      
      let bundle = Bundle(for: CardView.self)
      let nib = UINib(nibName: String(describing: CardView.self), bundle: bundle)
      let view = nib.instantiate(withOwner: self, options: nil)[0] as! CardView
      
      let users = userResults[Int(index)]
      
      view.profileImage.sd_setImage(with: URL(string: users.userProfileURL))
      view.nameLabel.text = users.userName
      view.uid = users.uid
      
      return view
      

      }

    I now have a working "Tinder Card with geoLocating" :)