使用 Swift 2.0 更新 UIButton 图像
Update UIButton image using Swift 2.0
当我尝试使用 更新 UIButton 图像 (Xcode 7 beta 1) 时,出现此错误
Cannot invoke 'setImage' with an argument list of type '(UIImage?, forState: nil)'
代码如下:
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: nil)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: nil)
}
我该如何解决这个问题?
forState
参数不能是 nil
- 它必须是 UIControlState
。
在你的情况下,你应该使用 UIControlState.Normal
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
}
在这里我们可以用 .Normal 替换 UIControlState.Normal(两者都可以很好地使用 .Normal 因为这将是 swift 速记功能)
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: .Normal)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: .Normal)
}
当我尝试使用
Cannot invoke 'setImage' with an argument list of type '(UIImage?, forState: nil)'
代码如下:
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: nil)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: nil)
}
我该如何解决这个问题?
forState
参数不能是 nil
- 它必须是 UIControlState
。
在你的情况下,你应该使用 UIControlState.Normal
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
}
在这里我们可以用 .Normal 替换 UIControlState.Normal(两者都可以很好地使用 .Normal 因为这将是 swift 速记功能)
if counter % 2 == 0{
playButton.setImage(UIImage(named: "pause"), forState: .Normal)
}
else if counter % 2 == 1 {
playButton.setImage(UIImage(named: "play"), forState: .Normal)
}