Using same swift UIlabels for different function values

Sadeed Ahmad

I am programming an iOS app using swift where i have 3 UILabels which will show data different sensors data into the same corresponding labels.

These are 3 labels which i am using.

@IBOutlet weak var xAccel: UILabel!
@IBOutlet weak var yAccel: UILabel!
@IBOutlet weak var zAccel: UILabel!

I am using UIsegmentedControl to change the display of data which is as follows.

@IBAction func AccelDidChange(_ sender: UISegmentedControl) {
        
       switch sender.selectedSegmentIndex {
        case 0:
            myAccelerometer()
            break
        case 1:
           myGyroscope()
           break
        default:
            myAccelerometer()
        }

Above used 2 functions are as follows

 func myAccelerometer() {
        // sets the time of each update
        motion.accelerometerUpdateInterval = 0.1
        
        //accessing the data from the accelerometer
        motion.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
            // can print the data on the console for testing purpose
            //print(data as Any)
            if let trueData = data {
                self.view.reloadInputViews()
                
                //setting different coordiantes to respective variables
                let x = trueData.acceleration.x
                let y = trueData.acceleration.y
                let z = trueData.acceleration.z
                
                
                //setting the variable values to label on UI
                self.SensorName.text = "Accelerometer Data"
                self.xAccel.text = "x : \(x)"
                self.yAccel.text = "y : \(y)"
                self.zAccel.text = "z : \(z)"
                
            
            }
        }
    }

func myGyroscope() {
            motion.gyroUpdateInterval = 0.1
            motion.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
    
                if let trueData = data {
                    self.view.reloadInputViews()
                    
                    //setting different coordiantes to respective variables
                    let x = trueData.rotationRate.x
                    let y = trueData.rotationRate.y
                    let z = trueData.rotationRate.z
    
                    //setting the variable values to label on UI
                    self.SensorName.text = "Gyroscope Data"
                    self.xAccel.text = "x: \(x)"
                    self.yAccel.text = "y: \(y)"
                    self.zAccel.text = "z: \(z)"
                }
            }
        }

** Problem is it keeps on displaying both the Accelerometer and Gyroscope data on UILabels at the same time instead of only showing the data of a particular sensor when tapped. I have tried to use the break option but still not working. If any one could point out the possible solution, that would be great. Thanks **

EIDT - Here is the output on screen where you can see the values fluctuates between different sensors. I only want readings from 1 sensor at a time. https://imgur.com/a/21xW4au

Alexander Nikolaychuk
@IBAction func AccelDidChange(_ sender: UISegmentedControl) {
     
    switch sender.selectedSegmentIndex {
     case 0:
         motion.stopGyroUpdates()
         myAccelerometer()
         break
     case 1:
        motion.stopDeviceMotionUpdates()
        myGyroscope()
        break
     default:
         myAccelerometer()
     }
    
}

You need to stop unneeded resource before the switch. Try this please

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift: Using argument as default value for a different argument in the same function

Same function with same inputs returns different values

Same function shows different values at different places

Using different namespace in same function

How to assign different object values to different buttons while using the same function?

5 different Objects with the same values to an function

how to mock different values for same function jest?

Call same function on two different objects in Swift

Update same column with multiple values at different positions using Overlay function in Postgres

How to create multiple columns at once using same function with different argument values in R?

Python dictionaries using same keys but different values

Mocking different values for the same module using Jest

Values are same but names are different using linq

How to mock the same function called in different components with different returned values

Lisp: same mathematical function evaluates to different values at different time?

How to compare two different values of different functions or same function?

How to plot multiple bar plots with same x values, different y values, and final product is similar to that of using facet_wrap function?

Using the same function on two different controllers

How to parallelize the same function using different arguments?

Using the same function for different IDs to show elements

Using loop to repeat the same function for different datasets

Use my Swipe gesture function on multiple UILabels swift

Using the same values with different classes in different @media without duplicating

Using the same CSS values as in Figma design, produce different values

call function multiple times using different values

Using different row values to create a function

Function using for loop returns different values

Can I bind different associated values in a Swift enum to the same var?

same Int has different values in Swift, mysterious riddle