我在 Swift 中工作,我正在尝试创建一个简单的 IBAction,它在单击按钮后将图像(我已经加载到我的 Assets 中)放入 UIImageView。
我已经控制拖动并连接了所有东西,但我收到了这个错误: “无法将表达式类型 '()' 转换为类型 'UIImage!'” 我是初学者,所以请彻底解释所有答案。 我的代码:
@IBAction func colorBtnClicked(sender: UIButton) {
colorsPanel.image = "Red_colors_panel"
}
最佳答案
首先,您需要将 UIImage
传递给 .image
属性,但是有一个 String
对象,这会导致错误。
要从项目包中读取图像,您需要按以下方式执行操作:
colorsPanel.image = UIImage(named: "Red_colors_panel.png") // and do not forget the extension
关于ios - Swift:单击按钮后将图像放入 UIImageView 的 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24726652/