It doesn't seem possible to create nested custom subviews using .xib's created with Interface Builder. For example I want to create a custom BarView
and nested inside of that a custom FooView
where the contents of each view is created in IB and stored in
a .xib file:
The BarView
is loaded from its .xib file and added as a subview in the ViewController
's viewDidLoad()
method:
class ViewController: UIViewController {
var barView : BarView?
override func viewDidLoad() {
super.viewDidLoad()
self.barView =
(UINib(nibName: "BarView", bundle:Bundle.main).
instantiate(withOwner: nil, options: nil).first as? BarView)
self.view.addSubview(self.barView!)
}
}
I can create and connect to an outlet for the UIButton
created by IB:
class BarView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSLog("BarView init:NSCoder, barButton set: \(barButton != nil)")
}
public override func awakeFromNib() {
super.awakeFromNib()
NSLog("BarView: awakeFromNib(), barButton set: \(barButton != nil)")
}
@IBOutlet weak var barButton: UIButton!
}
I can can also connect outlets and actions to the nest FooView
:
class FooView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSLog("FooView init:NSCoder, fooButton set: \(fooButton != nil)")
}
public override func awakeFromNib() {
super.awakeFromNib()
NSLog("FooView: awakeFromNib(), fooButton set: \(fooButton != nil)")
}
@IBAction func doFoo(_ sender: Any) {
NSLog("doFoo")
}
@IBOutlet weak var fooButton: UIButton!
}
I can see the init?(NSCoder)
and awakeFromNib()
methods are indeed being called. I don't know why BarView
's method are called
twice:
... BarView init:NSCoder, barButton set: false
... BarView: awakeFromNib(), barButton set: false
... FooView init:NSCoder, fooButton set: false
... BarView init:NSCoder, barButton set: false
... BarView: awakeFromNib(), barButton set: true
... FooView: awakeFromNib(), fooButton set: false
The contents of the FooView
(i.e. the fooButton
) does not show up and is nil
in the FooView
both callbacks. Is nesting custom view's created with independent .xib files supported? If so, what is the trick to accomplishing this?
The various suggestions in this post are not quite right or don't work.
It doesn't seem possible to create nested custom subviews using .xib's created with Interface Builder.
Bear in mind that you're swimming against the current here; storyboards were introduced exactly to get away from having lots of separate .xib files.
I can see the init?(NSCoder) and awakeFromNib() methods are indeed being called. I don't know why BarView's method are called twice
They're called twice because you're creating two instances. One is in Main.storyboard
, and the other is in BarView.xib
.
The contents of the FooView (i.e. the fooButton) does not show up and is nil in the FooView both callbacks
This sounds like it's probably another case of mistaken identity, similar to the problem above. There's an instance of Foo
in BarView.xib
that's different from the one in FooView.xib
.
Is nesting custom view's created with independent .xib files supported? If so, what is the trick to accomplishing this?
Not directly, as far as I remember. You can certainly load a view from a .xib file and add it to your view graph, though. You could even create a UIView
subclass whose job is to load the view in a specified .xib and set it as its own subview. One thing you'll want to pay attention to is the File's Owner proxy object in the .xib file; be sure to understand how you can use it to connect objects in your .xib file to outlets in the file's owner.