osx - Initialising custom control both programmatically and from NIB -
i writing custom ui control, , curious standard practices around initialisation (particularly dependance on control's delegate). controls, control relies on delegate provide important information how render itself.
when control used within nib, initwithcoder:
, awakefromnib
methods correctly called , delegate nicely set through iboutlet
. awakefromnib
calls secondary method called setupcontrol
interacts delegate set control. life's good!
however, when create control manually using initwithframe:
, awakefromnib
never called. curious how other developers deal supporting both ib , programmatic control creation. can see couple of techniques supporting programmatic case:
- force developer call
initwithframe:
, followedsetdelegate:
, followedsetupcontrol
. not onerous, expose internal workings of control (ie. have knowsetupcontrol
) - modify
initwithframe:
method take delegate well. encapsulate inner workings bit more, however, not sure particularly commonly used idiom (and seems manipulating subviews based on delegate responses deferred until bit later in view's lifecycle, rather in initialiser) - override
setdelegate:
accessor callsetupcontrol
method. not big fan of this, smacks of programming side-effect.
just thought i'd throw out there see techniques have overlooked. thoughts?
thanks. craig
first : -awakefromnib
automaticaly sent controls once nib loaded. means once entire ui encapsulated in nib loaded cocoa send -awakefromnib
ui components. reason send after loading avoid nil iboutlet. normaly if add controls programmatically it's done after nib unpacking , after ui items loaded (assuming connect iboutlet custom control). there no potential issue calling -awakefromnib
@ end of -initwithframe:
method.
and : expect calling -setdelegate:
when creating control programmatically. -initwithframe:
+ -setdelegate:
isn't in order functional control created think ;)
Comments
Post a Comment