0

I am writing a custom UI control, and am curious as to the standard practices around initialisation (particularly dependance on the control's delegate). Like most controls, my control relies on the delegate to provide important information about how to render itself.

When the control is used from within a NIB, the initWithCoder: and awakeFromNib methods are correctly called and the delegate is nicely set up through the IBOutlet. awakeFromNib calls a secondary method called setupControl which interacts with the delegate to set up my control. Life's good!

However, when I create the control manually using initWithFrame:, obviously awakeFromNib is never called. I am curious how other developers deal with supporting both IB and programmatic control creation. I can see a couple of techniques for supporting the programmatic case:

  • Force the developer to call initWithFrame:, followed by setDelegate:, followed by setupControl. Not too onerous, but does expose the internal workings of my control (ie. they have to know about setupControl)
  • Modify the initWithFrame: method to take the delegate as well. This does encapsulate the inner workings a bit more, however, I am not sure that this is a particularly commonly used idiom (and it seems that manipulating subviews based on delegate responses is something that is normally deferred until a bit later in the view's lifecycle, rather than in the initialiser)
  • Override the setDelegate: accessor to call the setupControl method. Not a big fan of this, as it smacks of programming by side-effect.

Just thought I'd throw it out there to see what techniques I have overlooked. Thoughts?

Thanks. Craig

Craig Edwards
  • 2,118
  • 1
  • 18
  • 23

1 Answers1

0

First : -awakeFromNib is automaticaly sent to controls once the NIB is fully loaded. That means that once the entire UI encapsulated in NIB is loaded Cocoa send -awakeFromNib to all UI components. The only reason it send this after loading is to avoid nil IBOutlet. Normaly if you add controls programmatically it's done after NIB unpacking and after all UI items are loaded (assuming you connect IBOutlet to your custom control). So there is no potential issue calling -awakeFromNib at the end of your -initWithFrame: method.

And : Everyone expect calling -setDelegate: when creating control programmatically. -initWithFrame: + -setDelegate: isn't to much in order to get a functional control created I think ;)

Pyroh
  • 216
  • 2
  • 13
  • However, when you say there is no issue calling `awakeFromNib` from `initWithFrame:`, in the programmatic world the delegate won't be set via the `IBOutlet`. It would be set manually in code - which is OK, but it would happen **after** `initWithFrame:` is called, which would mean that `initWithFrame:` can't reliably call `awakeFromNib` because it relies on the delegate being set. It is this chicken/egg dilemma I am wrestling with. – Craig Edwards Apr 10 '13 at 04:19
  • As far as I understand your control rely on its delegate in its init and setup phase ? A delegate is optional and couldn't have to be required in order to initialize anything. You have a design issue here. – Pyroh Apr 10 '13 at 08:28