I'm trying to do a simple control to simulate "android tabs" that I can use in xcode interface designer.
So I made a simple XIB which has few interface objects (scroll, stack, uiview...) and gave him a class called "TabsCustomControl"
my problem now is to know how I should do the setup/initialize this xib/UIView class.
In my mind it makes sense that you pass parameters to the xib/custom UIView when its initialized so it knows what sizes/colors/images the tabs should have based on the count of tabs.
But as soon I run
let tabsCustomControl = Bundle.main.loadNibNamed("TabsCustomControl", owner: self, options: nil)!.first as! TabsCustomControl
I dont have a chance to config it
tabsCustomControl.tabs = ["first tab"]
it runs the init... but I still haven't defined the property with tab list [String] so now what happens is that the code is run without any information regarding the tabs.
@IBInspectable var tabs : [String] = []
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup()
{
// THIS IS Empty, so loop is not running
for _ in tabs {
let newTab = UIButton()
newTab.backgroundColor = UIColor.green
newTab.setTitle("Test", for: .normal)
horizontalStackView.addArrangedSubview(newTab)
}
}
I can fix this by running setup() function after the class is initialized and parameters set, but this seems bad since i will use it in many places... i will be stuck with calling the .setup() each time after I create the xib
what approach should i be using?