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
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
Swift: Failed to Assign Value to a Property of Protocol
Applicationwillterminate: Not Being Called
New Itunes Connect Interface -- Should It Immediately Be Seen on "Prerelease"
How to Effectively Process Uitouches in a Multitouch Sequence
Cabasicanimation Rotate Returns to Original Position
Swift 3: Replace C Style For-Loop with Float Increment
Detect Touch on Child Node of Object in Spritekit
What's the State of Developing iOS Apps in Linux
iOS Swift: Could Not Cast Value Type '_Nscfnumber' to 'Nsstring'
Converting Int to Float Loses Precision for Large Numbers in Swift
Uicolor Colorwithred:Green:Blue:Alpha: Always Returns White Unless One Argument Is 0
How Can Split from String to Array by Chunks of Given Size
Main.Async VS Main.Sync() VS Global().Async in Swift3 Gcd
Simulate Universal Gravitation for Two Sprite Kit Nodes
How to Find the Address of a Stack Trace in Lldb for iOS
Push Notification -Didfinishlaunchingwithoptions
iOS Facebook Sdk - Post Open Graph and Show on Timeline Without Clicking Activity Log