SegmenControl Use XIB file call -- FirstController & SecondController





import UIKit

class SegmentTestViewController: UIViewController {
    @IBOutlet weak var segmentC: UISegmentedControl!
    @IBOutlet weak var containerView    : UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        add(asChildViewController: FSegViewController())
     
    }
    
    private func add(asChildViewController viewController: UIViewController) {
        
        // Add Child View Controller
        addChildViewController(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(toParentViewController: self)
    }
    
    //----------------------------------------------------------------
    
    private func remove(asChildViewController viewController: UIViewController) {
        // Notify Child View Controller
        viewController.willMove(toParentViewController: nil)
        
        // Remove Child View From Superview
        viewController.view.removeFromSuperview()
        
        // Notify Child View Controller
        viewController.removeFromParentViewController()
    }
    
    @IBAction func segaction(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            remove(asChildViewController: SSegViewController())
            add(asChildViewController: FSegViewController())
        case 1:
            remove(asChildViewController: FSegViewController())
            add(asChildViewController: SSegViewController())
        default:
            break
        }
    }
    
}





No comments: