选择/取消选择按钮swift xcode 7

Select/deselect buttons swift xcode 7(选择/取消选择按钮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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)