Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have a speed label that displays the speed of the user in the default mph. I have a slide up settings menu with two buttons on it "kilometers" & "milesPerHour" and am trying to toggle between kmh and mph. The code looks correct as I'm checking if the buttons have been selected which should then convert the speed into the respective kmh or mph. However the code below produces no effect. Any ideas on what might be wrong?


Swift
//MPH and KMH
@IBOutlet weak var SpeedLbl: UILabel! 


//Outlets for buttons on the slide up settings menu
@IBOutlet weak var kilometers: UIButton!
@IBOutlet weak var milesPerHour: UIButton!



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        
        if milesPerHour.isSelected == true {
            SpeedLbl.text = String(format: "%.1f", location.speed) + " mph"
        }
        else if kilometers.isSelected == true {
            SpeedLbl.text = String(format: "%.1f", location.speed * 1.61) + " kph"
        }
    }


What I have tried:

I created actions for the two buttons but found it doesn't work because the speed can only be retrieved in the did update locations method, so trying to access the users speed outside of it doesn't seem to work which is why I am using the outlets of the buttons to check if they've been pressed.
Posted
Comments
Maciej Los 20-Feb-17 14:52pm    
Do you need to use buttons? I've read that ToggleGroup would be much, much better for your requirements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900