Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to link two view controllers into one container view and switch between them using segmented control?

Tags:

ios

swift

xcode6

I got one view controller that contain 1 segmented control and 2 UI views. But I think it's too complicated to update the UI view for enhancement for future editing. I'm using hidden method.

import UIKit

class PopularHistoryViewController: UIViewController {


    @IBOutlet weak var segmentedControl: UISegmentedControl!
    @IBOutlet weak var popularView: UIView!
    @IBOutlet weak var historyView: UIView!

    @IBAction func indexChanged(sender: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex
        {
        case 0:
            NSLog("Popular selected")
            //show popular view
            popularView.hidden = false
            historyView.hidden = true
        case 1:
            NSLog("History selected")
            //show history view
            popularView.hidden = true
            historyView.hidden = false
        default:
            break; 
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

}

What I want is 1 container view that contain 2 controller views so I can switch them using segmented control.

enter image description here

like image 763
Nurdin Avatar asked Jan 17 '15 19:01

Nurdin


People also ask

How do you change view controllers using segmented control?

To switch between the child view controllers, we use a segmented control. Click the + button in the top right to bring up the Library and add a segmented control to the navigation bar of the master view controller. Open MasterViewController. swift and create an outlet for the segmented control.


1 Answers

The other approach is to only have one child view controller in memory at one time, and then upon changing the selected value in the segmented control, load the new child view controller, transition between one child view controller to the next, and then remove the old child view controller:

let viewControllerIdentifiers = ["first", "second"]  // storyboard identifiers for the child view controllers

@IBAction func didChangeValue(sender: UISegmentedControl) {
    let newController = storyboard!.instantiateViewController(withIdentifier: viewControllerIdentifiers[sender.selectedSegmentIndex])
    let oldController = childViewControllers.last!

    oldController.willMove(toParentViewController: nil)
    addChildViewController(newController)
    newController.view.frame = oldController.view.frame

    transition(from: oldController, to: newController, duration: 0.25, options: .transitionCrossDissolve, animations: {
        // nothing needed here
    }, completion: { _ -> Void in
        oldController.removeFromParentViewController()
        newController.didMove(toParentViewController: self)
    })
}

Obviously, this assumes that you've already got the first child view controller already on the view (easily done if you use the "container view" control in Interface Builder) and the default selected value for the UISegmentedControl matches. You also have to have storyboard identifiers for these two child scenes.

For Swift 2 rendition, see previous revision of this answer.

like image 131
Rob Avatar answered Sep 18 '22 03:09

Rob