1

I was thinking of either viewDidLoad or inside my view subclass in awakeFromNib (since I load the view from nib). From the design point of view, encapsulating the customisation inside the view subclass sounds better. What are other options?

Meda
  • 2,776
  • 4
  • 20
  • 31
  • awakeFromNib sounds better than viewDidLoad. What other options you want to know? – Anoop Vaidya Jan 28 '13 at 04:52
  • what about -(void)viewDidAppear:(BOOL)animated{ } & -(void)viewWillAppear:(BOOL)animated{ } ? – DD_ Jan 28 '13 at 04:53
  • Well, if there are no better option, I don't want to hear them, only if there is something even better than `awakeFromNib`. One downfall in using it is that it will not get called if you don't use nibs (or will it)? – Meda Jan 28 '13 at 04:54
  • @MilKyWaY I would like to keep my customisation inside the view subclass if possible, would like to avoid the controller. – Meda Jan 28 '13 at 04:55
  • You should find the answer to your problem [here](http://stackoverflow.com/questions/377202/which-should-i-use-awakefromnib-or-viewdidload). It details when you should use both methods you've mentioned. – Alex Smith Jan 28 '13 at 05:02

1 Answers1

1

For me:

  1. If the UI changes require coordination with the app's model, I generally do it in the view controller as I like to think of that as the gatekeeper between the model and the view. If it's creation of controls, I may do it in viewDidLoad, sometimes viewDidAppear. If it's re-layout of stuff based upon orientation changes, I'll do it in viewWillLayoutSubviews.

  2. For anything of complexity that does not require extensive interaction with the model, I'll do in the view subclass.

  3. The obvious other alternative it to design it in Interface Builder (with the appropriate autosizing masks and/or autolayout constraints) so you don't need to do anything programmatically. Often people are doing layout changes based upon orientation or size of the control that could have been taken care of automatically through judicious autosizing settings or autolayout constraints. Clearly this is often not possible, but don't overlook these if you're just adjusting layout based upon the size of the main view.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • if you are to do it in your subclass, would you choose `awakeFromNib` as a starting point? Does it get called if I change my mind and init the view in code? (not in nib) I get your point, regarding the view controller (1), but in my case, is simply skinning. – Meda Jan 28 '13 at 05:00
  • @Meda I've always just done the adding of controls in `initWithFrame` (or with storyboards, `initWithCoder`), adjusted frames in `layoutSubviews` or have done custom drawing in `drawRect`. See the links provided by others (and the [UIView subclassing notes](http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-DontLinkElementID_2)) for discussions about which methods to override. Sorry I can't provide better counsel regarding `awakeFromNib`, but hopefully the links provided by others will help you on that. – Rob Jan 28 '13 at 15:11