Created
September 13, 2022 18:10
-
-
Save ayaysir/0b4e2ab7ec5f77103dbf279989fffa23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContainerViewController.swift | |
// Example-ContainerView | |
// | |
// Created by yoonbumtae on 2022/09/13. | |
// | |
import UIKit | |
// 2-1: 대리자 프로토콜 작성 | |
protocol ContainerVCDelegate: AnyObject { | |
func didReceivedValueFromContainer(_ controller: ContainerViewController, value: String) | |
} | |
class ContainerViewController: UIViewController { | |
// 2-2: 딜리게이트 변수 설정 | |
// ContainerVCDelegate를 준수한다면 어느 클래스나 구조체도 여기에 할당될 수 있음 | |
weak var delegate: ContainerVCDelegate? | |
// 1-4: IBOutlet 연결(컨테이너 뷰 컨트롤러) | |
@IBOutlet weak var lblReceivedValueFromRoot: UILabel! | |
// 2-3: 텍스트필드 @IBOutlet 연결 | |
@IBOutlet weak var txfSendValueToRoot: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
// 2-4: 컨테이너 뷰로 값 전송 기능 구현 | |
@IBAction func btnActSubmitToRoot(_ sender: Any) { | |
// 2-5: 딜리게이트를 통해 | |
delegate?.didReceivedValueFromContainer(self, value: txfSendValueToRoot.text ?? "") | |
} | |
// 1-5: Root로부터 값을 전달받는 함수 생성 | |
func setLabel(_ value: String) { | |
// 1-6: Root로부터 받은 값을 레이블에 설정 | |
lblReceivedValueFromRoot.text = value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RootViewController.swift | |
// Example-ContainerView | |
// | |
// Created by yoonbumtae on 2022/09/13. | |
// | |
import UIKit | |
class RootViewController: UIViewController { | |
// 1-0: @IBOutlet 연결(루트 뷰 컨트롤러) | |
@IBOutlet weak var txfSendValueToContainer: UITextField! | |
// 2-0: @IBOutlet 연결(루트 뷰 컨트롤러) | |
@IBOutlet weak var lblReceivedValueFromContainer: UILabel! | |
// 1-1: 컨테이너 뷰 컨트롤러를 참조하는 변수 생성 | |
private var containerVC: ContainerViewController? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
// 1-7: 전송 버튼 이벤트 설정 | |
@IBAction func btnActSubmitToContainer(_ sender: Any) { | |
containerVC?.setLabel(txfSendValueToContainer.text ?? "") | |
} | |
// 1-2: segue.identifer별 분기 설정 | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
switch segue.identifier { | |
case "ContainerSegue": | |
// 1-3: segue.destination(컨테이너 뷰 컨트롤러)을 containerVC에 지정 | |
containerVC = segue.destination as? ContainerViewController | |
// 2-6: containerVC의 delegate 변수를 RootVC 자신(self)으로 지정 | |
containerVC?.delegate = self | |
default: | |
break | |
} | |
} | |
} | |
// 2-7: ContainerVCDelegate를 준수(conform)하는 확장 구현 | |
extension RootViewController: ContainerVCDelegate { | |
func didReceivedValueFromContainer(_ controller: ContainerViewController, value: String) { | |
lblReceivedValueFromContainer.text = value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment