0

I have a segmented control, that change some calculations. When I had the control in the same VC as the rest of the app, it works, but I want to make a "settings page".

How can I make the selected state of the segmented control in "settingsVC" effect the "mainVC"?

From the "mainVC"

// Metric or imperial system segmented control 

    let argumentForSegment = metricImperialSegment.selectedSegmentIndex

    if argumentForSegment == 0 {
        statusLabel.text = "\(kmtLabel)"
        topSpeedLabel.text = "\(topSpeedForLabel) kmh"
        averageSpeedLabel.text = "\(averageSpeedForLabel) kmh"
        altitudeLabel.text = "\(altitudeNoDecimals) m"
        kmhMphLabel.text = "KMH"
    }

    if argumentForSegment == 1 {
        statusLabel.text = "\(mphForLabel)"

From the "settingsVC"

import UIKit

let argumentForSegment = metricImperialSegment 

class settingsVC: UIViewController {

    @IBOutlet weak var metricImperialSegment: UISegmentedControl!

    @IBAction func backButton(_ sender: Any) {
        performSegue(withIdentifier: "toMainView", sender: self)
    }
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
Paal Aune
  • 353
  • 5
  • 10
  • post the notification from setting VC to main VC – Saurabh Jain Sep 02 '17 at 16:06
  • Thanks. Can you tell me how to of that? – Paal Aune Sep 02 '17 at 17:49
  • You can also make a settingsVC protocol and delegate it to your mainVC. This way, if there is more than settings action effecting your mainVC you can add a function to handle each action individually. I added a gist with an example since I can't add an answer https://gist.github.com/mmr118/3fc87f2bf542892863e8f1c04d92f429 – mmr118 Sep 02 '17 at 18:11

0 Answers0