Select/deselect buttons swift xcode 7(选择/取消选择按钮swift xcode 7)
问题描述
学习 swift 已经完成了一部分,但我又遇到了一个小问题,我确信我在这方面有点新,并且有一个简单的解决方案,但我无法弄清楚如何选择/下面的取消选择按钮是我到目前为止所拥有的,它是一个按钮在点击时变成复选标记......我已经做到了那么远,但我需要在再次点击时取消选择该按钮,然后显然能够再次点击如果需要的话.
Part way done with learning swift but I hit a small wall and yet again, I'm sure I'm just a bit new at this and an easy solution is there but I'm having trouble figuring out how to select/deselect buttons below is what I have so far and it is a button turns into a checkmark when clicked on... I've gotten that far but I need that button to deselect when clicked on again and then obviously be able to be clicked again if need be.
@IBAction func buttonPressed(sender: AnyObject) {
sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}
推荐答案
Swift 3 注意:.selected
和 .checked
现在是小写 UIControlState 值,部分方法已重命名:
Swift 3 note: .selected
and .checked
are now lower case UIControlState
values in the SDK, and some of the methods have been renamed:
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
您现在还可以在 Xcode 8 中使用图像文字而不是 UIImage(named:)
:
You can also now use image literals with Xcode 8 instead of UIImage(named:)
:
#imageLiteral(resourceName: "Unchecked")
斯威夫特 2:
为什么不用按钮的.Selected
状态作为选中"状态,.Normal
状态作为未选中"状态.
Why not use the .Selected
state of the button as the "checked" state, and the .Normal
state as the "unchecked" state.
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
// ...
@IBAction func buttonPressed(sender: AnyObject) {
if let button = sender as? UIButton {
if button.selected {
// set deselected
button.selected = false
} else {
// set selected
button.selected = true
}
}
}
这篇关于选择/取消选择按钮swift xcode 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选择/取消选择按钮swift xcode 7
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01