기어가더라도 제대로
[UIKit-기초] Xib 파일에 선언되어있는 UIViewController를 불러오기 본문
지난번 Xib 파일에 선언된 UIView를 불러오는 글에 이어서 UIViewController를 불러오는 일을 하려함
2025.01.26 - [UIKit 기초] - [UIKit-기초] Xib에서 코드로 UIView 파일을 불러오기
- UIStoryboard: UIViewController들의 선언과 플로우를 볼 수 있는 파일
- UIViewController의 편집에 적합하다고 생각
- Xib: Xcode Interface Builder의 약자로 UI를 그리는데 사용
- UIView 등의 커스텀한 버전을 꾸미는데 적합
- Nib: Next Interface Builder의 약자로, Xib의 이전 버전
- 참고로 Next는 스티브 잡스가 애플에서 쫓겨났던 시기에 만든 컴퓨터 회사이름
- UIStoryboard로 UIViewController를 불러오는게 적합하다면서 Xib에 선언된 UIViewController를 불러오는 이유
- Swift 파일을 생성할 때, 기본적으로 "Also create xib file" 옵션이 선택되어있어서 이 파일이 생기는데, 여기다 UIViewController를 선언해서 사용하는 경우가 있어서 정리하고자 함

Xib 파일에서 선언되어있는 UIViewController를 불러오기
선언부 XibViewController
- Xib 파일을 생성

- XibViewController.xib 파일을 만들면 기본적으로 UIView 요소가 마치 UIViewController인양 자리함

- 확인을 위해서 Xib VC 라는 레이블을 하나 선언하고 auto layout을 잡음
- X Center, Y Center를 잡음

- File's Owner를 클릭하고 인스펙터 영역에서 XibViewController와 연결

- outlets 연결에서 View를 View에 연결

- cmd + n으로 XibViewController.swift를 생성

- "Also create XIB file" 체크 해제하고 서브클래스를 UIViewController로 선언함

- XibViewController.swift는 기본형 그대로 사용
import UIKit
class XibViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
호출부 ViewController
- 기본으로 제공되는 ViewController 클래스에서 XibViewController를 생성하고 화면 전환하는 버튼 선언
- 마찬가지로 Center X,Y 로 Auto Layout

- 버튼을 ViewController에 액션 연결

- showXibButtonTapped 함수 완성
@IBAction func showXibButtonTapped(_ sender: UIButton) {
let xibViewController = XibViewController(
nibName: "XibViewController",
bundle: nil
)
self.present(xibViewController, animated: true, completion: nil)
}
- 완성 모습

UIStoryboard에서 UIViewController를 불러오기
- Xib로 불러온 김에 "Main" UIStoryboard에 선언된 "MyViewController"라는 가상의 뷰컨트롤러를 불러오는 코드를 남김
- MyViewController는 required init?(coder: Coder)가 선언되어있어야할지 궁금, 아시는 분 덧글로 남겨주세요.
if let viewController = UIStoryboard(name: "Main", bundle: nil)
.instantiateViewController(withIdentifier: "MyViewController") as? MyViewController {
// Use the view controller
}
전체 코드
https://github.com/Damagucci-Juice/ExXibViewController
GitHub - Damagucci-Juice/ExXibViewController
Contribute to Damagucci-Juice/ExXibViewController development by creating an account on GitHub.
github.com
참조
- dengApro의 답변
A view can only be associated with at most one view controller at a time (UISegmentedControl)
Hello The error occurs in the simulator on iOS 6. Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view
stackoverflow.com
Loaded nib but the 'view' outlet was not set
I added a new nib file to my project, and tried to load it. However, when I click on the toolbar icon that is supposed to take me to the view that I created, I get an NSInternalInconsistencyExcept...
stackoverflow.com
'UIKit 기초' 카테고리의 다른 글
[UIKit-기초] MVVM 인풋-아웃풋 패턴(with. Combine) (0) | 2025.02.11 |
---|---|
[UIKit-기초] UINavigationBar Cheat Sheet - 2 (0) | 2025.02.08 |
[UIKit-기초] UINavigationBar Cheat Sheet - 1 (0) | 2025.02.07 |
[UIKit-응용] Tab 전환을 모달로 하기(UITabBarController, Modal, Sheet) (0) | 2025.02.04 |
[UIKit-기초] UISegmentedControl - basic (0) | 2025.02.03 |