본문 바로가기
#모바일 [Mobile]/iOS

[iOS] 뷰 컨트롤러 (View Controller)

by cy_mos 2021. 10. 29.
반응형
카테고리 (Category) 작성 날짜 (Write Date) 최근 수정 날자 (Recent Write Date) 작성자 (Writer)
iOS 2021.10.29. 15:53 2021.10.29. 15:53 Dev.Yang

 

⚓️ 뷰 컨트롤러 (View Controller)

애플리케이션의 근간을 이루는 객체로, 모든 앱은 적어도 하나 이상의 뷰 컨트롤러 (View Controller)로 구성이 되어있습니다. 이들의 주된 역할은 화면을 구성하는 요소인 뷰 (View)를 관리하거나 화면과 데이터 사이의 상호작용을 관리하는 애플리케이션의 중추적인 역할을 수행합니다.

 

뷰 컨트롤러 (View Controller)는 두 가지의 대분류로 나눌 수 있습니다.

 

  • 콘텐츠 뷰 컨트롤러 (Contents View Controller)
  • 컨테이너 뷰 컨트롤러 (Container View Controller) 

⚓️ 콘텐츠 뷰 컨트롤러 (Contents View Controller)

 

콘텐츠 뷰 컨트롤러 (Contents View Controller)

 

이미지, 텍스트, HTML 등 애플리케이션 화면에 표현할 콘텐츠를 관리하는 컨트롤러를 말합니다. 또한, 콘텐츠 뷰 컨트롤러 (Contents View Controller)는 화면 전체 사이즈의 루트 뷰 (RootView)를 내장하며 루트 뷰 (RootView) 위에 각종 컨텐츠를 추가합니다.

 

  • Storyboard에서 UIButton이나 UILabel 등을 배치하는 데에 사용되는 ViewController 역시 콘텐츠 뷰 컨트롤러 (Contents View Controller) 라고 합니다.
  • ViewController를 정의하는 기본적인 클래스는 UIViewController 이며, 해당 클래스는 뷰를 관리하고 이벤트를 처리하고 ViewController에서 다른 ViewController로 전환하는 작업을 수행합니다.

 

아래의 항목들은 콘텐츠 뷰 컨트롤러 (Contents View Controller)의 커스텀 뷰 컨트롤러 (Custom ViewController) 목록들입니다.

 

 

콘텐츠 뷰 컨트롤러 (Contents View Controller) 세부적인 생명 주기는 Displaying and Managing Views with a View Controller 문서에서 확인할 수 있습니다.

더보기

UIKit gives you several opportunities to configure your view controller and views before displaying them onscreen. When you instantiate your view controller from a storyboard, UIKit creates that object using its init(coder:) method.

 

When you present a view controller onscreen, UIKit must first load and configure the corresponding views, which it does using the following sequence of steps: 

 

  1. It creates each view using the view’s init(coder:) method. 
  2. It connects views to the corresponding actions and outlets in the view controller.
  3. It calls the awakeFromNib() method of each view and the view controller. 
  4. It assigns the view hierarchy to view controller’s view property.
  5. It calls the view controller’s viewDidLoad() method. 

 

At load time, perform only the one-time configuration steps you need to prepare the view controller for use. Use load time to create and configure any additional views that aren't part of your storyboard. Don't perform tasks that must happen each time your view controller appears onscreen. For example, don't start animations or update the values of views. 

Perform any final view-related tasks shortly before your views appear onscreen. UIKit notifies the owning view controller when its views are about to appear onscreen, and updates the layout of those views to fit the current environment, by calling the following methods in order: 

 

  1. It updates the view controller's trait collection based on the target window.
  2. It calls the viewWillAppear(_:) method to let you know the view controller’s view is about to appear onscreen. 
  3. If needed, it updates the current layout margins and calls the viewLayoutMarginsDidChange() method.
  4. If needed, it updates the safe area insets and calls the viewSafeAreaInsetsDidChange() method.
  5. It calls the viewWillLayoutSubviews() method.
  6. It updates the layout of the view hierarchy.
  7. It calls the viewDidLayoutSubviews() method. 
  8. It displays the views onscreen.
  9. It calls the view controller’s viewDidAppear(_:) method.

 

Update the content of your views in the viewWillAppear(_:) method of your view controller. Changing the content of many views triggers an automatic layout update, so making changes in that method avoids an additional layout step. When making changes, you may use the view controller's traitCollection property to access information about the current environment, such as the display scale or the vertical and horizontal size classes. Although you can access traits earlier than viewWillAppear(_:), traits aren't guaranteed to be final until that method. In iOS 12 and earlier, UIKit only provides a partial set of traits at load time, and doesn't provide a complete set of traits until the viewWillAppear(_:) method. For more information about the available traits, see UITraitCollection.


⚓️ 컨테이너 뷰 컨트롤러 (Container View Controller)

 

컨테이너 뷰 컨트롤러 (Container View Controller)

 

ViewController와 ViewController의 연결 관계를 관리하는 컨트롤러입니다. 또한, 컨테이너 뷰 컨트롤러 (Container View Controller)는 Navigation을 이용하여 다음 화면을 단계적으로 접근하는 방법, Tab을 이용하여 병렬적으로 접근하는 방법 또는 MainDisplay과 DetailDisplay로 나누어 접근하는 등의 다양한 접근 방법을 제공합니다.

 

* 컨테이너 뷰 컨트롤러 (Container ViewController)의 제어 하에 있는 ViewController는 자식 뷰 컨트롤러 (Child ViewController)라고 부릅니다.

* 컨테이너 뷰 컨트롤러 (Container ViewController)와 직접 연결 된 자식 뷰 컨트롤러 (Child ViewController)를 루트 뷰 컨트롤러 (Root ViewController)라고 부릅니다.

* 컨테이너 뷰 컨트롤러 (Container ViewController)도 필요에 따라서 자식 뷰 컨트롤러 (Child ViewController)로 사용될 수 있습니다.

 

 

아래의 항목들은 컨테이너 뷰 컨트롤러 (Container View Controller)의 커스텀 뷰 컨트롤러 (Custom ViewController) 목록들입니다.

 

 

컨테이너 뷰 컨트롤러 (Container ViewController) 내부 동작원리에 대하여 세부적인 내용은 Creating a Custom Container View Controller 문서에서 확인할 수 있습니다.


🚀 REFERENCE

 

Apple Developer Documentation

 

developer.apple.com

 

꼼꼼한 재은 씨의 스위프트 실전편

[꼼꼼한 재은 씨의 스위프트 실전편] 개정판 출간 SWIFT 5, XCODE 11, IOS 13 반영전편을 학습하지 않았더라도 기본적인 프로그래밍 경험이 있는 사람이라면 누구나 이해할 수 있도록 쉽고 자세하게 설

book.naver.com

반응형

댓글