我有UISegmentedController
。当其值发生变化时,将执行以下代码:
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)")
以下是我连续点击每个片段时的输出:
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
您可以看到在设置后立即读取tintColor会显示selectedSegmentIndex
为1和2的错误值。
如果我将整个if / else转换为switch()语句,它将按预期工作。
另一个数据点,如果我在每个if子句中将backgroundColor
设置为不同的颜色,则背景会相应更改,但tintColor
不会如上所述发生变化。< / p>
这是在Xcode 6.0.1的发行版中运行的。发生了什么事?
答案 0 :(得分:2)
您正在测试相等性,而不是分配它。你应该只使用一个等号:
sender.tintColor = self.green
我的猜测是,当你将它作为一个开关实现时,你并没有犯同样的错误