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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33906060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:17:29  来源:igfitidea点击:

Select/deselect buttons swift xcode 7

iosswiftuibuttonswift2xcode7

提问by yrpalnoob

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.

学习 swift 的部分方法已经完成,但我又碰到了一个小墙,我确定我对此有点陌生,有一个简单的解决方案,但我无法弄清楚如何选择/取消选择下面的按钮是我到目前为止所拥有的,它是一个按钮在点击时变成一个复选标记......我已经走了那么远,但我需要在再次点击时取消选择该按钮,然后显然可以在需要时再次点击.

@IBAction func buttonPressed(sender: AnyObject) {
    sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}

回答by JAL

Swift 3 note: .selectedand .checkedare now lower case UIControlStatevalues in the SDK, and some of the methods have been renamed:

斯威夫特3注:.selected.checked现在是小写UIControlState的SDK值,以及一些方法已经改名:

let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)

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

您现在还可以在 Xcode 8 中使用图像文字,而不是UIImage(named:)

#imageLiteral(resourceName: "Unchecked")

Swift 2:

斯威夫特 2:

Why not use the .Selectedstate of the button as the "checked" state, and the .Normalstate as the "unchecked" state.

为什么不使用.Selected按钮的状态作为“选中”状态,将.Normal状态作为“未选中”状态。

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
        }
    }
}

回答by Nitin Gohel

You dont need to set selected in condition. I just doing with following method in swift:

您不需要在条件中设置 selected 。我只是在 swift 中使用以下方法:

func selectDeselect(sender: UIButton){

        sender.selected = !sender.selected

        if(sender.selected == true)
        {
        sender.setImage(UIImage(named:"select_heart"), forState: UIControlState.Normal)

        }
        else
        {
        sender.setImage(UIImage(named:"heart"), forState: UIControlState.Normal)
        }

    }

回答by Nikunj Kumbhani

Here is Working code for swift 4.

这是 swift 4 的工作代码。

Make Sure you need to connect Button IBActionOutlet as UIButtonand set default button image from storyboard whatever you want.

确保您需要将按钮IBAction插座连接为UIButton并从故事板中设置您想要的默认按钮图像。

@IBAction func btnTapped(_ sender: UIButton) {

    if sender.currentImage == UIImage(named: "radio_unchecked"){

        sender.setImage(UIImage(named: "radio_checked"), for: .normal)

    }else{

        sender.setImage(UIImage(named: "radio_unchecked"), for: .normal)
    }
}

回答by Archana SIngh

Set uncheck image on default state from storyboard and check image on selected state from storyboard.

从情节提要中取消选中默认状态下的图像,并从情节提要中选中选中状态上的图像。

@IBAction func buttonPressed(sender: AnyObject) {
   buttonOutlet.isSelected = !buttonOutlet.isSelected
}