ios - Swift:单击按钮后将图像放入 UIImageView 的 IBAction

标签 ios swift uiimageview ibaction

我在 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/

相关文章:

ios - 无法弄清楚如何管理 MBProgressHUD 和调度

ios - ViewController 显示不是来自 origin 0 的对象

swift - Eventbrite 重定向 URI iOS OAuthSwift

ios - 将 uiimage 设置为 nil 不会使用 ARC 释放内存

ios - 在tableview ios6.1中加载图像

ios - 尝试插入第 3 节,但更新后只有 3 节

ios - CSS 动画变换 : translate poor performance on iPad

ios - Storyboard 中 UINavigationController 的弹出窗口大小

ios - 允许在 TableView 行选择时执行条件操作

ios - 视网膜和非视网膜兼容的 UIImage 堆叠和定位