ios - 每次用户单击时快速更改按钮的图像

标签 ios swift uibutton uiimage

我在表格 View 单元格中有一个按钮。我希望按钮最初有一个图像“A”,当用户点击它时,它变成“B”,当用户再次点击它时它又变回“A”。

在这个场景中让两张图片分别为“A”和“B”

最佳答案

无论按钮在哪里:

button.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
button.setImage(UIImage(named: "a.png")!, forState: .Normal)
button.tag = 999

func pressed(sender: UIButton!) {
     if sender.tag == 999 {
          sender.setImage(UIImage(named: "b.png")!, forState: .Normal)
          sender.tag = 0
     } else {
          sender.setImage(UIImage(named: "a.png")!, forState: .Normal)
          sender.tag = 999
     }
}

关于ios - 每次用户单击时快速更改按钮的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947532/

相关文章:

ios - UILocalNotification 自定义 soundName 未播放

json - 我正在尝试使用循环获取存储在 JSONData 中的轨道名称

ios条件tableview单元格渲染

ios - 如何在 UIButton 中设置图像大小?

ios - 如何从 NSmutableDictionary 中删除值(不是键)

ios - 如何优化通过 Parse.com 查询填充的 UICollectionView?

iphone - UIAlertView 渲染错误

swift - 怎么去掉 map 上的绘制路线

ios - Swift Playgrounds 和 Playground 书籍

iphone - 单击时更改 UIButton 文本