How to Programmatically Add a Uisegmentedcontrol to a Container View

How can I use segmented control from container view to control my main view

I've found solution for this.

class FirstTestTableViewController: UITableViewController, ScrollSegmentDelegate {

func segmentIsChosen(segment: Int) {
print(segment)
questionNumber = segment + 1
}

var questionNumber = 0

func updateQuestionNumber() {
questionNumber + 1
scrollSegmentVC?.questionNumberToReceive = questionNumber
}

var scrollSegmentVC: ScrollSegmentVC?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segment" {
scrollSegmentVC = segue.destination as? ScrollSegmentVC
scrollSegmentVC?.delegate = self
}
}

Loading a UIViewController into a Container View programmatically

Make sure this @IBOutlet var customContainer: UIView! is linked to the containerview and not to the vc's main view in storyboard

Swift how to use container view and segmented control

You got very common mistake and error. But don't worry.

Check your full error message. There must be a clue what variable you are trying to access and getting nil (it might be resultLabel).

Then make sure the connection between storyboard and view controller for this variable is correct. You can try to remove and create it again if you don't get what is wrong.

If you need more help, please show us full error message.

UISegmentedControl programmatically

Just call function self.segmentController(mySegmentControl) after you change the selected index.

UISegmentedControl text programmatically

[segmentedControl setTitle:<YourLocalizedString> forSegmentAtIndex:0];

Change views using Segmented Control

Here i have created a complete solution as per your requirement.

Swift 4

//
// SegementedVC.swift
//
// Created by Test User on 01/02/18.
// Copyright © 2018 Test User. All rights reserved.
//

import UIKit

class SegementedVC: UIViewController {

//----------------------------------------------------------------
// MARK:-
// MARK:- Outlets
//----------------------------------------------------------------

@IBOutlet weak var segmentControl : UISegmentedControl!
@IBOutlet weak var containerView : UIView!

//----------------------------------------------------------------
// MARK:-
// MARK:- Variables
//----------------------------------------------------------------

private lazy var firstViewController: FirstViewController = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

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

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

return viewController
}()

private lazy var secondViewController: SecondViewController = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

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

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

return viewController
}()

//----------------------------------------------------------------
// MARK:-
// MARK:- Abstract Method
//----------------------------------------------------------------

static func viewController() -> SegementedVC {
return UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SegementedView") as! SegementedVC
}

//----------------------------------------------------------------
// MARK:-
// MARK:- Memory Management Methods
//----------------------------------------------------------------

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

//----------------------------------------------------------------
// MARK:-
// MARK:- Action Methods
//----------------------------------------------------------------

@IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
updateView()
}

//----------------------------------------------------------------
// MARK:-
// MARK:- Custom Methods
//----------------------------------------------------------------

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()
}

//----------------------------------------------------------------

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

//----------------------------------------------------------------

func setupView() {
updateView()
}

//----------------------------------------------------------------
// MARK:-
// 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)
}
}

Storyboard Screenshots

enter image description here

enter image description here

enter image description here

How do I use a UISegmentedControl to switch views?

The simplest approach is to have two views that you can toggle their visibility to indicate which view has been selected. Here is some sample code on how it can be done, definitely not an optimized way to handle the views but just to demonstrate how you can use the UISegmentControl to toggle the visible view:



- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

if (selectedSegment == 0) {
//toggle the correct view to be visible
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else{
//toggle the correct view to be visible
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}



You can of course further re-factor the code to hide/show the right view.



Related Topics



Leave a reply



Submit