상세 컨텐츠

본문 제목

[iOS] ViewController Life Cycle

iOS

by caution.dev 2018. 12. 14. 17:26

본문

뷰의 상태 변화 메서드

뷰가 나타나거나 사라지는 뷰가 화면에 보이는 상태가 변화할때 호출되는 메서드입니다.


Apple Document 

- UIVIewController - UIKit 

- Tutorial : Working With ViewController


  • func viewDidLoad()
    • 계층이 메모리에 로드된 직후 호출되는 메서드
    • 뷰의 추가적인 초기화 작업을 하기 좋은 시점
    • 메모리에 처음 로딩 될때 1 호출되는 메서드로, 메모리 경고로 뷰가 사라지지 않는 이상 다시 호출되지 않음
  • func viewWillAppear(_ animated: Bool)  
    • 뷰가 계층에 추가되고 화면이 표시되기 직전에 호출되는 메서드
    • 뷰의 추가적인 초기화 작업을 하기 좋은 시점
    • 다른 뷰로 이동했다가 되돌아오면 재호출되는 메서드로, 화면이 나타날때마다 수행해야하는 작업을 하기 좋은 시점
  • func viewDidAppear(_ animated: Bool)  
    • 뷰가 계층에 추가되어 화면이 표시되면 호출되는 메서드
    • 뷰를 나타내는 것과 관련된 추가적인 작업을 하기 좋은 시점
  • func viewWillDisappear(_ animated: Bool) 
    • 뷰가 계층에서 사라지기 직전에 호출되는 메서드
    • 뷰가 생성된 발생한 변화를 이전상태로 되돌리기 좋은 시점
  • func viewDidDisappear(_ animated: Bool)
    • 뷰가 계층에서 사라진 호출되는 메서드
    • 뷰를 숨기는 것과 관련된 추가적인 작업을 하기 좋은 시점
    • 시간이 오래 걸리는 작업은 하지 않는 것이 좋음


뷰의 레이아웃 변화 메서드

뷰가 생성된 바운드 위치 등의 레이아웃에 변화가 발생했을 호출되는 메서드입니다. 

  • func viewWillLayoutSubviews()
    • 뷰가 서브뷰의 레이아웃을 변경하기 직전에 호출되는 메서드
    • 서브뷰의 레이아웃을 변경하기 전에 수행할 작업을 하기 좋은 시점
  • func viewDidLayoutSubviews()
    • 서브뷰의 레이아웃이 변경된 호출되는 메서드
    • 서브뷰의 레이아웃을 변경 추가적인 작업을 수행하기 좋은 시점


  1. initWithCoder:
    • When using storyboards, the controller is initialized with this method, not with the init or initWithNibName:bundle: methods.
  2. awakeFromNib
  3. willMoveToParentViewController:
    • The controller that is passed with this call is the UINavigationController.
  4. prefersStatusBarHidden
  5. preferredStatusBarUpdateAnimation
  6. loadView
    • UIViewController‘s loadView function assigns all objects with ‘Referencing Outlets’ (aka IBOutlets) in Interface Builder to their corresponding IBOutlet properties. If you need access to these IBOutlet objects in this function, call super first.
  7. prepareForSegue:sender:
    • This call allows us to inspect the UIStoryboardEmbedSegue that embeds the smaller scene in Scene B’s container view.
  8. viewDidLoad
    • This method is usually where most of a controller’s set up happens. Note that all of our IBOutlets have been connected, but our views have not yet been laid out.
  9. extendedLayoutIncludesOpaqueBars
  10. edgesForExtendedLayout
  11. viewWillAppear:
  12. extendedLayoutIncludesOpaqueBars
  13. edgesForExtendedLayout
  14. updateViewConstraints
  15. viewWillLayoutSubviews
  16. viewDidLayoutSubviews
  17. Animation
    • The animation that transitions from Scene A to Scene B runs. Step 18 does not happen until the animation finishes.
  18. viewDidAppear:
  19. didMoveToParentViewController:
    • This call concludes the process started on step 2. Here we receive the same instance of UINavigationController.
  20. updateViewConstraints
  21. viewWillLayoutSubviews
  22. viewDidLayoutSubviews


iOS 6.0에서 부터 viewDidUnload 쓰이면서 swift-based UIVIewController에서는 dealloc() 쓰이지 않는다.

대신 deinit() 쓰면댐




관련글 더보기