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

查看:138
本文介绍了选择/取消选择swift xcode 7按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分方式完成学习快速,但我打了一个小墙又一次,我相信我只是有点新的,一个简单的解决方案,但我无法弄清楚如何选择/取消选择下面的按钮是我到目前为止所有按钮在点击时变成一个复选标记...我已经走得那么远但我再次点击时需要该按钮取消选择然后显然可以再次点击如果需要的话。

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(名称:)

You can also now use image literals with Xcode 8 instead of UIImage(named:):

#imageLiteral(resourceName: "Unchecked")

Swift 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天全站免登陆