iOS(Swift) - UIViewController LifeCycle
안녕하십니까? 엘리아입니다! 오늘은 UIViewController 생명주기에 대해 글을 써보려 합니다. iOS 프로그래밍에서 정말 중요한 부분이고 반드시 알고 있어야 합니다.
혹시라도 내용에 틀린 부분이 있다면 언제든 댓글로 알려주세요!
init(coder: NSCoder), init(frame: CGRect), init(nibName: String?, bundle: Bundle?)
초기화를 담당합니다. init(coder: NSCoder)는 storyboard를 사용하여 ViewController를 만들 경우 가장 먼저 호출되는 부분입니다. 그리고 init(frame: CGRect)는 code로 ViewController를 만드는 경우 가장 먼저 호출됩니다. init(nibName: String?, bundle: Bundle?)는 nib파일로 viewController를 만들 때 호출하여 viewController를 초기화합니다.
loadView()
view를 만드는 역할을 하는 메서드입니다.
viewDidLoad()
viewController가 메모리에 load 되고 난 후 호출되는 메서드입니다. 화면이 처음 만들어질 때 한 번만 실행되기 때문에 처음 한 번만 실행해야 하는 초기화 코드를 이 메서드 내부에 작성하면 됩니다. (viewController 생명주기에 한 번만 호출됨)
viewWillAppear(_ animated: Bool)
view가 나타나기 직전에 호출되는 메서드입니다.
viewDidAppear(_ animated: Bool)
view가 나타나면 호출되는 메서드입니다.
viewWillDisappear(_ animated: Bool)
view가 사라지기 직전에 호출되는 메서드입니다.
viewDidDisappear(_ animated: Bool)
view가 사라지고 나서 호출되는 메서드입니다.
deinit()
viewController가 메모리에서 해제될 때 호출됩니다.
원래는 deinit() 전에 viewDidUnload()라는 메서드가 있어서 viewController를 메모리에서 해제할 때 호출되는데 iOS 6 이상부터는 viewDidUnload() 메서드를 사용하지 않습니다.. Swift는 view가 더 이상 필요하지 않으면 인스턴스를 자동으로 할당 해제한다고 하네요..!
Deinit is it a good practice to implement it on viewControllers?
I am wondering if it is a good practice to implement a deinit on every view controller to check if it is correctly removed when it disappears and avoiding leaking memory?
stackoverflow.com
여기까지 UIViewController LifeCycle입니다.
다음에는 또 다른 글로 찾아오겠습니다. 긴 글 읽어주셔서 감사합니다!

참고자료
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621495-viewdidload/
Apple Developer Documentation
developer.apple.com
https://baked-corn.tistory.com/32
[ios] UIViewController Lifecycle
UIViewController Lifecycle 오늘은 UIViewController 의 Lifecycle에 대해 알아보는 시간을 갖도록 하겠습니다. View Controller들은 iOS 어플리케이션들에서 중추적인 역할을 하고 여러분이 만드는 모든 어플리..
baked-corn.tistory.com