I have a subclass of UIToolbar with a member variable for one of the controls that will display on the toolbar. After creating the toolbar, I call a method that adds the control to the toolbar. When I create the control and assign it to the member variable, I get a fatal nil error. However, when i create it and assign it to a local variable it succeeds. While I have it working with a local variable, I'd like to understand the reason this adjustment works. Any insights would be much appreciated.
categoryCtl = VGSegment(frame: placeholder.frame, segmentConfiguration: config, titles: titles)
categoryCtl.delegate = delegate
let tbButton = UIBarButtonItem(customView: categoryCtl)
Above fails at line 3 with fatail nil error. However, if i change it as follows, it succeeds.
let segment = VGSegment(frame: placeholder.frame, segmentConfiguration: config, titles: titles)
segment.delegate = delegate
categoryCtl = segment
let tbButton = UIBarButtonItem(customView: segment)
Line 3 succeeds and program works as expected.