Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I set up in the `init` method versus the `viewDidLoad` method? [duplicate]

I'm creating a UIViewController subclass, and I create it with a custom init function, initWithImages:(NSArray *)images, that then returns the view controller for use.

I'm confused however what I'm supposed to do in the init versus the viewDidLoad, viewWillAppear, etc. functions. In this init function I set up the image view, a caption label and give it gesture recognizers, but are those things that should only be done when it loads or appears?

Should the init be as small as possible? What do I put in each is basically my question?

I'm using the UIViewControllers with a UIPageViewController so it's important that even with a bunch of view controllers in the page view controller that memory isn't used absurdly, so I want to make sure I'm doing this right.

like image 915
Doug Smith Avatar asked Oct 20 '25 16:10

Doug Smith


1 Answers

init should be used for data related basic initialisation. viewDidLoad Should be used for view based initialisation, because the view has been created at that point in time so you can add subviews. There is no point creating subviews if you can't use them yet.

like image 124
Wain Avatar answered Oct 23 '25 06:10

Wain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!