IOS application/Swift

20. swift change multiple view by segmented control

개발자킹콩 2021. 6. 9. 14:48

각 Segment control에 의해 다른 뷰를 보여줘야 하는 경우가 존재한다.

 

 

View가 비슷한 구조라면 Segment control에 의해 띄우는 데이터를 달리하면 된다.

하지만 다른 View가 등장해야 하면 그에 따른 설정이 필요하다.

 

 

 


 

 

 

Storyboard (Home.storyboard)

 

 

publicViewController에 연결된 UIView이다. 

내부에는 View - Segment Control - ContainerView 가 존재한다.

이때, SegmentControl의 index에 의해 ContainerView에 보여질 View를 달리하면 된다.

 

 

 


 

 

Storyboard (Public.storyboard)

 

 

여기에는 두 가지 ViewController가 존재한다.

ImmediationViewController과 ReservationViewController이다.

각각의 Label과 Button은 가로, 세로 중앙에 위치하도록 했다. --> 보여지는 View의 크기를 알아보기 위함이다.

 

 

 

이때 주의할 점은 Custom Class와 Storyboard ID 를 각각 지정해야 한다는 점이다.

 

 

 

 


 

 

 

 

코드 구현

 

import UIKit

class PublicViewController: UIViewController {
    
    let viewModel: PublicViewModel = PublicViewModel()
    
    //----------------------------------------------------------------
    // MARK:- Outlets
    @IBOutlet weak var segmentControl: UISegmentedControl!
    @IBOutlet weak var containerView: UIView!
    

    //----------------------------------------------------------------
    // MARK:- View Life Cycle Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    //----------------------------------------------------------------
    // MARK:- Abstract Method
    static func viewController() -> PublicViewController {
        return UIStoryboard.init(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "PublicViewController") as! PublicViewController
    }

    //----------------------------------------------------------------
    // MARK:- Memory Management Methods
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //----------------------------------------------------------------
    // MARK:- Variables
    private lazy var immediationViewController: ImmediationViewController = {
        // Load Storyboard
        let storyboard = UIStoryboard(name: "Public", bundle: Bundle.main)

        // Instantiate View Controller
        var viewController = storyboard.instantiateViewController(withIdentifier: "ImmediationViewController") as! ImmediationViewController

        // Add View Controller as Child View Controller
        self.add(asChildViewController: viewController)

        return viewController
    }()

   private lazy var reservationViewController: ReservationViewController = {
        // Load Storyboard
        let storyboard = UIStoryboard(name: "Public", bundle: Bundle.main)

        // Instantiate View Controller
        var viewController = storyboard.instantiateViewController(withIdentifier: "ReservationViewController") as! ReservationViewController

        // Add View Controller as Child View Controller
        self.add(asChildViewController: viewController)

        return viewController
   }()

    //----------------------------------------------------------------
    // MARK:- Action Methods
    @IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
        updateView()
    }


    //----------------------------------------------------------------
    // MARK:- Custom Methods
    private func add(asChildViewController viewController: UIViewController) {

        // Add Child View Controller
        addChild(viewController)

        // Add Child View as Subview
        containerView.addSubview(viewController.view)

        // Configure Child View
        viewController.view.frame = containerView.bounds
        viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        // Notify Child View Controller
        viewController.didMove(toParent: self)
    }

    private func remove(asChildViewController viewController: UIViewController) {
        // Notify Child View Controller
        viewController.willMove(toParent: nil)

        // Remove Child View From Superview
        viewController.view.removeFromSuperview()

        // Notify Child View Controller
        viewController.removeFromParent()
    }

    private func updateView() {
        if segmentControl.selectedSegmentIndex == 0 {
            remove(asChildViewController: reservationViewController)
            add(asChildViewController: immediationViewController)
        } else {
            remove(asChildViewController: immediationViewController)
            add(asChildViewController: reservationViewController)
        }
    }

    func setupView() {
        updateView()
    }
}