Programaticcally pressing a button

Hi everyone, I am trying to write my first app, its slow progress but interesting.

I have an app that displays 10 buttons when I touch one of the buttons it changes the image on that individual button, this all goes through the same routine based on the view tag (each button 0 to 9) That bit is working fine

What I want to do is have another button that when pressed will update the original 10 buttons with a new image

I am sure this must be possible but I do not yet think that I have the skillset

Thanks in advance

so its the same logic as your button press but it applies to all buttons?

if so then you should try doing it (update image) inside a for loop

Hi, thanks for your advise, that is what I have been trying to do with no luck :slight_smile:

Here is my code, maybe not the best code that you have ever seen

@IBAction func Pin(_ sender: UIButton) {
        let pinButton = sender.tag
        if start[pinButton] == true {
            check [pinButton] = 0
            start[pinButton] = false  }

        switch check [ pinButton ] {
        case 1 :
            sender.setImage(UIImage(named: "\(sender.tag+1) Pin.png"), for: UIControl.State.normal)
            check [ pinButton ] = 0
            sender.setImage(UIImage(named: "\(sender.tag+1) Pin Y.png"), for: UIControl.State.normal)
            check [ pinButton ] = 1

The first block of code is just to find which image is being shown currently, the rest of the code then swaps the image with its other one

What I could not work out how to do in the loop was update the correct image, the new button that I pressed to update all I gave it .tag = 10 and that is the button that it updated!, I am guessing that I need to change the sender in sender.setimage but can’t figure out how?

Thanks for your help

i dont really get it, the code you showed does not show a loop, its only 1 set button based on button press

Hi, and thanks again for your reply

There are multiple buttons attached to the IBAction

I have created a array based on the stacks and then as you suggested update the buttons through IBOutput, I now have it doing what I need

Thanks for your help

1 Like