6

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)
}
JAL
  • 41,701
  • 23
  • 172
  • 300
yrpalnoob
  • 1,149
  • 3
  • 8
  • 5

7 Answers7

8

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)

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

#imageLiteral(resourceName: "Unchecked")

Swift 2:

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
        }
    }
}
Community
  • 1
  • 1
JAL
  • 41,701
  • 23
  • 172
  • 300
  • What do you mean by set selected and set deselected? – yrpalnoob Nov 25 '15 at 00:16
  • A UIButton can have multiple "states" with different images. You can set an image for both the selected and normal states, and chose when you want the button to show each image by changing its state. – JAL Nov 25 '15 at 00:25
  • And how would that look? This is very helpful, thank you. – yrpalnoob Nov 25 '15 at 00:41
  • Why don't you try it and see what it looks like? – JAL Nov 25 '15 at 01:07
  • I am. I'm having trouble understanding set selected and set deselected. I keep getting a "value of type 'anyObject' has no member 'selected' error. – yrpalnoob Nov 25 '15 at 01:37
  • Sorry, I realized that there were some typos with my answer. I edited it so that the code will now compile. Different states allow you to customize the appearance of a button. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/ – JAL Nov 25 '15 at 01:56
  • lol sorry. I didn't have any time last week to try this out... Thanksgiving is a full time engagement in my family... I put the let button = UIButton()... in viewDidLoad and changed out the "unchecked" and "checked" with the .pngs, and attached the button to the IBAction. Anddd I'm getting nothing. What am I doing wrong? – yrpalnoob Dec 01 '15 at 00:57
  • Where to set this button action? in cellforrow or didselect ? – deltami Jan 07 '18 at 09:28
4

You dont need to set selected in condition. I just doing with following method in 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)
        }

    }
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
2

Here is Working code for swift 4.

Make Sure you need to connect Button IBAction Outlet as UIButton and set default button image from storyboard whatever you want.

@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)
    }
}
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
2

update for 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
               }
           }
}
Erik Peruzzi
  • 535
  • 1
  • 5
  • 8
1

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
}
0
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()
}
Arafin Russell
  • 1,487
  • 1
  • 18
  • 37
0

To select only the pressed button out of three buttons and deselect the others when designated button is pressed.

@IBAction func buttonPressed(_ sender: UIButton) {
    
    // Deselect all buttons
    button1.isSelected = false
    button2.isSelected = false
    button3.isSelected = false
    // Select the pressed button
    sender.isSelected = true
    
}
D. Mika
  • 2,577
  • 1
  • 13
  • 29
GOKuLkrish
  • 11
  • 3