A Right Place to Setup Element in UIView Child Class

Submitted by swarut on Sun, 10/21/2012 - 15:59

Just want to put a short note about this as I  often make a mistake.

When we create a custom view, there are some tweak/setup we want to do. For example, we want to change the font of all UILabel elements or setting the size of UIImage element, and so on. Where is the right place to put the setup stuffs? There are two places for use to do so. First is in initWithCoder method, and second is in awakeFromNib. The difference between these two methods is that for initWithCoder, all the properties within the view is still nil as they did not yet get bound to the element in .nib. In contrast, in awakeFromNib method, the properties are available. 

Thus, to determine where is the right place, ask yourself what you want to do. If you want to do a setting which is not required the view elements to be presented, do it in initWithCoder. If you want to do a setting for ui element, go with awakeFromNib.