화면을 구성하는 뷰를 직접 구현하고 관련된 이벤트를 처리하는 뷰 컨트롤러
child view controller
를 가지고 있다.child view controller
를 관리하고 레이아웃과 화면 전환을 담당한다.child view controller
에서 한다.container view controller
는 대표적으로 navigation controller
와 tabBar controller
가 있다.계층 구조로 구성된 contents를 순차적으로 보여 주는 container view controller
Navigation Stack
사용
세부적으로,
Show seguement 사용
Back Button 구현
@IBAction func tapBackButton(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
Present Modally seguement 사용
Back Button 구현
@IBAction func tapBackButton(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
@IBAction func tapCodePushButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "CodePushViewController") else { return }
self.navigationController?.pushViewController(viewController, animated: true)
}
다음 화면에 해당하는 View Controller 인스턴스화
Back Button 구현
@IBAction func tapBackButton(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
@IBAction func tapCodePresentButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "CodePresentViewController") else { return }
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true, completion: nil)
}
다음 화면에 해당하는 View Controller 인스턴스화
Back Button 구현
@IBAction func tapBackButton(_ sender: UIButton) {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
Code로 Push 부분에 구현
@IBAction func tapCodePushButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "CodePushViewController") as? CodePushViewController else { return }
viewController.name = "Park"
self.navigationController?.pushViewController(viewController, animated: true)
}
다음 화면에 해당하는 View Controller에 접근하기 위해 다운캐스팅 진행
작업을 위임하는 느낌
다음 화면으로 넘어가기 위한 코드에서 아래의
viewController.delegate = self
이런 식으로 선언을 해줘서 다음 화면의 정보를 가지고 현재 컨트롤러에 구현된 코드를 이용해 결국 다시 돌아왔을 때의 화면을 구성하겠다는 느낌
아래 코드 확인
Segueway를 실행하기 직전에 시스템에 의해 자동으로 prepare 메소드가 호출되도록 오버라이딩해서 사용
이를 이용하여 다음 화면에 해당하는 view controller에 데이터 전달
아래 코드 확인
ViewController Code
import UIKit
class ViewController: UIViewController, SendDataDelegate {
@IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapCodePushButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "CodePushViewController") as? CodePushViewController else { return }
viewController.name = "Park"
viewController.delegate = self
self.navigationController?.pushViewController(viewController, animated: true)
}
@IBAction func tapCodePresentButton(_ sender: UIButton) {
guard let viewController = self.storyboard?.instantiateViewController(identifier: "CodePresentViewController") as? CodePresentViewController else { return }
viewController.modalPresentationStyle = .fullScreen
viewController.name = "Park"
self.present(viewController, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let viewController = segue.destination as? SeguePushViewController {
viewController.name = "Park"
}
}
func sendData(name: String) {
self.nameLabel.text = name
self.nameLabel.sizeToFit()
}
}
CodePushViewController Code
import UIKit
protocol SendDataDelegate: AnyObject {
func sendData(name: String)
}
class CodePushViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
var name: String?
weak var delegate: SendDataDelegate?
override func viewDidLoad() {
super.viewDidLoad()
if let name = name {
self.nameLabel.text = name
self.nameLabel.sizeToFit()
}
}
@IBAction func tapBackButton(_ sender: UIButton) {
self.delegate?.sendData(name: "Park")
self.navigationController?.popViewController(animated: true)
}
}
이미지 리소스 추가
https://github.com/pjs0418/LEDBoard