5

Originally, my code was working.

IBAction func clickedon(_ sender: UIButton) {

    if(gamestate[sender.tag] == 0 && gameisactive == true){

        gamestate[sender.tag] = activeplayer

    if (activeplayer == 1){
        print("working")
        sender.setImage(UIImage(named:"Cross.png"), for: UIControlState()) <-----------
        activeplayer = 2
    }
    else {
        print("working2")
        sender.setImage(UIImage(named:"Nought.png"), for: UIControlState()) <-----------
        activeplayer = 1

    } 
    }

These two pieces of code that set image were working. These buttons have nothing set to their image. When they are clicked on, I want them to switch to those images. Those images are in the folder with the view controller. I am not sure why they stopped working. I commented out all other code to see if it could be an issue but it doesn't seem to be. The print statements get printed out in both cases. It's just the set image that doesn't seem to do anything. There is no error, it just does nothing.

I don't really understand the UIControlState() code. I am mimicking an online project that has the same code and it works. It is really strange. Please let me know if you have any suggestions!

Pang
  • 9,564
  • 146
  • 81
  • 122
Aditya Yadav
  • 81
  • 1
  • 1
  • 2
  • Your syntax looks correct, other than what Joey point out. At that point I might suggest "breaking up" those lines - maybe the issue will be exposed if you declare your UIImages separately. I know, things were working, but maybe the issue is elsewhere. At this point I'd even try changing the type of button from System to Custom or vice-versa. –  Oct 28 '16 at 03:00
  • Please post image of requirement and what you achieved in your code then others can help you – Vinodh Oct 28 '16 at 08:01

6 Answers6

4

IBAction func clickedon(_ sender: UIButton) {

if(gamestate[sender.tag] == 0 && gameisactive == true){

    gamestate[sender.tag] = activeplayer

if (activeplayer == 1){
    print("working")
   sender.setImage(UIImage(named: "Cross.png")!, forState: UIControlState.Normal)
   sender.setImage(UIImage(named:"CrossSelected.png")!, forState: UIControlState.Selected)

    activeplayer = 2
}
else {
    print("working2")
    sender.setImage(UIImage(named: "Nought.png")!, forState: UIControlState.Normal)
    sender.setImage(UIImage(named:"NoughtSelected.png")!, forState: UIControlState.Selected)
    activeplayer = 1

} 
}
Himali Shah
  • 181
  • 1
  • 8
3

You can try this :

let image = UIImage(named: "imagename.png")
yourButton.setBackgroundImage(image, for: .normal)

I try using other setImage calls but not working. I found this setBackgroundImage to let the image appear. Hope that it will helps you.

1

In your code, UIControlState() can be replaced with UIControlState.normal or UIControlState.highlighted.

https://developer.apple.com/reference/uikit/uicontrolstate

IBAction func clickedon(_ sender: UIButton) {

    if(gamestate[sender.tag] == 0 && gameisactive == true){

        gamestate[sender.tag] = activeplayer

    if (activeplayer == 1){
        print("working")
        sender.setImage(UIImage(named:"Cross.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Cross_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 2
    }
    else {
        print("working2")
        sender.setImage(UIImage(named:"Nought.png"), for: UIControlState.normal)
        sender.setImage(UIImage(named:"Nought_highlighted.png"), for: UIControlState.highlighted)
        activeplayer = 1

    } 
}
Joey
  • 2,912
  • 2
  • 27
  • 32
0

Are you forcing a width and height constraint? If not your buttons may be degenerate because they have no image inside of them. You can also set the frame when you set the image; I'd prefer autolayout.

  UIImage(named:"Cross.png")!
  sender.setImage(image, for: .normal)
  sender.frame.size = image.size
Josh Homann
  • 15,933
  • 3
  • 30
  • 33
0

I figured out the answer. The UIControlState.normal works, the issue was that the buttons were actually placed below my board. This meant I could click on them yet they wouldn't appear.

It explains why the print statements and everything worked without error. Thank you!

Aditya Yadav
  • 81
  • 1
  • 1
  • 2
0
self.button.setImage(UIImage(named: "namebutton"), for: .normal)

when you want to change image of button on swift 3 , you could try my solution, believe with me because i tried it and success . thanks

Ace
  • 219
  • 1
  • 3