选择/取消选择按钮swift xcode 7 [英] Select/deselect buttons swift xcode 7

查看:25
本文介绍了选择/取消选择按钮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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆