0

I have a segmented control in a SettingsController.

The segmented control can display temperature in Celsius or Fahrenheit.

I have a mainViewController where I have the temperature label which displays current temperature in celsius.

On click of the segmented control I want to convert the temperature to appropriate unit and display it in the mainViewController.

@IBOutlet weak var temperatureUnitSegmentedControl: UISegmentedControl!
var mainViewController : MainViewController?

@IBAction func temperatureSegmentedControlListener(sender: UISegmentedControl) {

        switch temperatureUnitSegmentedControl.selectedSegmentIndex
        {
        case 0:
            showTemperatureInCelsius()
        case 1:
            showTemperatureInFahrenheit()
        default:
            break;
        }
    }
    func showTemperatureInCelsius(){
        mainViewController?.setTempInCelsius()

    }
    func showTemperatureInFahrenheit(){
            mainViewController?.setTempInFahrenheit()
    }

In mainViewController I want to do the conversions and update the temperature label

func setTempInCelsius(){
        temperatureLabel.text = "In Celsius"
        log?.debug("Temperature Celsius\(batteryTemperatureLabel.text)")
    }

    func setTempInFahrenheit(){
        temperatureLabel.text = "In Fahrenheit"
        log?.debug("Temperature Fahrenheit\(batteryTemperatureLabel.text)")
    }

This code does not hit the functions in the mainViewController and value of the label does not get updated.

Any help will be appreciated. Thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
A.S
  • 798
  • 1
  • 10
  • 32
  • For starters: Where do you set the variable `mainViewController`? Also, I assume the first code block you show is in that `SettingsController`? My guess would be that you simply never set the variable to the controller you want. Oh, and if that's the parent, don't forget to make the var `weak`, or you'll run into a retain cycle. – Gero Oct 06 '16 at 07:54
  • Check the link it will solve your problem http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Joe Oct 06 '16 at 07:57

2 Answers2

0

You can use delegates or protocol. click here

if you have to update views then you can use nsnotificationcenter Click here

Community
  • 1
  • 1
Ranjana Dangol
  • 1,319
  • 2
  • 13
  • 24
  • You should add some code too, because links might break in future, or post it as a comment. :) – Bista Oct 06 '16 at 09:24
0

Yoa can simply send action to your destination view controller to set the values like UIApplication.sharedApplication().sendAction("setTempInCelsious", to: nil, from: nil, forEvent: nil)

Hope this helps.

Sachin Amrale
  • 277
  • 1
  • 9