views:

129

answers:

1

I have a simple custom view that is connected via outlet to a NIB. For this particular view, there are actions that I would like to perform on the view when it is initialized, no matter what NIB it is on.

Trouble is, neither the (id)init or the (id)initWithFrame:(CGRect)frame methods are getting called on the custom view.

Which method gets called on a UIView when it is instantiated from a NIB? I would just use the view controller and viewDidLoad method except that this particular view appears on a lot of different NIBs.

+3  A: 

You can use awakeFromNib for this kind of initialization. The regular initialization methods are called when the object is actually created by IB and then archived using NSCoding, so those methods are never called within your application. You could also override initWithCoder: which will be called, but I don't recommend it since other outlets may not be wired at that point.

Jason Coco
Perfect! For some reason I thought awakeFromNib was limited to UIViewController - once again, this proves that sometimes all it takes is a second pair of eyes :)
retailevolved
@retailevolved: Yeah, when a nib file is loaded, every object in the nib file that is unarchived (except for proxy objects--since those are not actually unarchived from the nib) will be sent an awakeFromNib message after all objects have been unarchived and all outlets have been wired.
Jason Coco