Search code examples
swiftxibiboutlet

Can I bind xib file to view controller via IBOutlet?


I have a ViewController, and I want to dynamically load different UIView based on UISegmentedControl.I designed 3 UIViews in xib file. And I have set their File's Owner's Custtom class to my view controller name in Identify Inspector.After I connect my xib to view controller code via IBOutlet, I add the connected view via "addSubview(view)" method in viewDidLoad method. But When I run it, the compiler tells me that the connected view is null.


Solution

  • Instead of making three different xib files. Make one xib file with all three views in it, plus a fourth "default" view. Each view should be connected to a different IBOutlet in your class.

    Then in your viewDidLoad figure out which view you want to display and addSubview it to your default view. enter image description here