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

xnifntxz  于 2022-11-06  发布在  Swift
关注(0)|答案(7)|浏览(238)

我已经学了一部分了,但我又碰到了一堵小墙,我敢肯定,我只是有点新,在这和一个简单的解决方案是有,但我有麻烦弄清楚如何选择/取消选择下面的按钮是我到目前为止,它是一个按钮变成了一个复选标记时点击...我'我已经到了这一步,但我需要该按钮取消选择时,再次点击,然后显然能够再次点击,如果需要的话。

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

8wtpewkr1#

斯威夫特3注:在SDK中,.selected.checked现在是小写的UIControlState值,并且其中一些方法已被重命名:

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

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


# imageLiteral(resourceName: "Unchecked")

雨燕2:
为什么不使用按钮的.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
        }
    }
}
gdx19jrr

gdx19jrr2#

你不需要在条件中设置选择。我只是在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)
        }

    }
col17t5w

col17t5w3#

这是swift 4的工作代码。

确保您需要将按钮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)
    }
}
h7wcgrx3

h7wcgrx34#

Swift 4+更新:

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 {
            if button.isSelected {
                   // set deselected
                button.isSelected = false
               } else {
                   // set selected
                button.isSelected = true
               }
           }
}
7dl7o3gd

7dl7o3gd5#

将图像设置为取消选中情节提要中的默认状态,并选中情节提要中的选定状态。

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

h7wcgrx36#

private(set) lazy var myButton: UIButton = {
    let button = UIButton()
    button.setImage(UIImage(named: "Unchecked"), for: .normal)
    button.setImage(UIImage(named: "Checked"), for: .selected)
    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

@objc 
func buttonTapped(sender: AnyObject) {
    sender.isSelected.toggle()
}
j2datikz

j2datikz7#

按下指定按钮时,仅选择三个按钮中按下的按钮,并取消选择其他按钮。

@IBAction func buttonPressed(_ sender: UIButton) {

    // Deselect all buttons
    button1.isSelected = false
    button2.isSelected = false
    button3.isSelected = false
    // Select the pressed button
    sender.isSelected = true

}

相关问题