leftspin
leftspin

Reputation: 2488

Segmented Control tintColor not setting correctly when used with if/else clause

I have a UISegmentedController. When its value changes, this code is executed:

    println(sender.selectedSegmentIndex)

        var x = 0

        if (sender.selectedSegmentIndex == 0)
        {
            println("set to \(self.yellow)")
            sender.tintColor = self.yellow
            println("right after \(sender.tintColor)")
            x = 1
        }
        else if (sender.selectedSegmentIndex == 1)
        {
            println("set to \(self.green)")
            sender.tintColor == self.green
            println("right after \(sender.tintColor)")
            x = 2
        }
        else if (sender.selectedSegmentIndex == 2)
        {
            println("set to \(self.blue)")
            sender.tintColor == self.blue
            println("right after \(sender.tintColor)")
            x = 3
        }

    println("what it actually got set to: \(sender.tintColor), x is \(x)")

Here is the output when I tap each segment in succession:

0
set to UIDeviceRGBColorSpace 1 0.83 0.13 1
right after UIDeviceRGBColorSpace 1 0.83 0.13 1
what it actually got set to: UIDeviceRGBColorSpace 1 0.83 0.13 1, x is 1
1
set to UIDeviceRGBColorSpace 0.38 0.85 0.38 1
right after UIDeviceRGBColorSpace 1 0.83 0.13 1
what it actually got set to: UIDeviceRGBColorSpace 1 0.83 0.13 1, x is 2
2
set to UIDeviceRGBColorSpace 0.34 0.73 0.85 1
right after UIDeviceRGBColorSpace 1 0.83 0.13 1
what it actually got set to: UIDeviceRGBColorSpace 1 0.83 0.13 1, x is 3

You can see that reading the tintColor right after setting shows the wrong value for selectedSegmentIndex of 1 and 2.

If I convert the entire if/else into a switch() statement, it works as expected.

One other data point, if I set the backgroundColor to a different color in each of the if clauses, the backgrounds change accordingly but the tintColor doesn't change as described above.

This is running in the release version of Xcode 6.0.1. What's going on?

Upvotes: 1

Views: 266

Answers (1)

drewag
drewag

Reputation: 94733

You are testing for equality, not assigning it. You should only be using a single equal sign:

sender.tintColor = self.green

My guess is that you were not making the same mistake when you had it implemented as a switch

Upvotes: 2

Related Questions