Skip to content

Instantly share code, notes, and snippets.

@arturlector
Last active April 26, 2021 09:52
Show Gist options
  • Save arturlector/c30ff516ce9eb2007418 to your computer and use it in GitHub Desktop.
Save arturlector/c30ff516ce9eb2007418 to your computer and use it in GitHub Desktop.
Цикл жизни UIViewController? UIViewController life cycle?

Цикл жизни UIViewController?

https://habrahabr.ru/post/129557/

Ответственность UIViewController

UIViewController согласно шаблону проектирования MVC обеспечивает взаимосвязь
модели и представления.

Ответсвенность:
* Управление отображением данных на вьюхе (представлении). Изменяются данные - изменяется вьюха.
* Управление пользовательскими взаимодействиями (событиями) на вьюхе (представлении). 
* Управление размерами и версткой интерфейсов вьюхи (представления).

Жизненный цикл (старая версия)

1. Создание (Инициализация)

- init
- initWithNibName:

2. Создание (Инициализация) view

- (BOOL)isViewLoaded
- loadView
- viewDidLoad
- (UIView *)initWithFrame:(CGRect)frame
- (UIView *)initWithCoder:(NSCoder *)coder

3. Обработка изменения состояния view:

- viewDidLoad
- viewWillAppear:(BOOL)animated
- viewDidAppear:(BOOL)animated
- viewWillDisapear:(BOOL)animated
- viewdidUnload

4. Обработка memory warning

- didReceiveMemoryWarning

5. Уничтожение

- viewDidUnload
- dealloc

Жизненный цикл (новая версия)

Новая версия iOS имеет 6 ступеней которые управляют жизненным циклом UIViewController:

1. loadView
2. viewDidLoad
3. viewWillAppear
4. viewWillLayoutSubviews
5. viewDidLayoutSubviews
6. viewDidAppear

1. loadView

This event creates the view that the controller manages. It is only called when the view controller is created programmatically. This makes it a good place to create your views in code.

2. viewDidLoad

The viewDidLoad event is only called when the view is created and loaded into memory. 
But the bounds for the view are not defined yet. This is a good place to initialize the objects that 
the view controller is going to use.

Вызывается когда view создана и загружена в память. Но границы view еще не определены.

Инициализация объектов которые viewcontroller будет использовать.

3. viewWillAppear

This event notifies the the view controller whenever the view appears on the screen.
In this step the view has bounds that are defined but the orientation is not set.

4. viewWillLayoutSubviews

This is the first step in the lifecycle where the bounds are finalized. If you are not using constraints or Auto Layout you probably want to update the subviews here.

5. viewDidLayoutSubviews

This event notifies the view controller that the subviews have been setup. It is a good place to make any changes to the subviews after they have been set.

6. viewDidAppear

The viewDidAppear event fires after the view is presented on the screen. Which makes it a good place to get data from a backend service or database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment