Images Button Outlet Collection and Action

1.3k Views Asked by At

I am working with buttons with an image on them. The central idea is there are 6 image buttons with default images on them and on click of any of those buttons: for example, if there are 1,2,3,4,5,6 buttons, on click of 1 the images for 2,3,4,5,6 and on click of 2 the images for 1,3,4,5,6 change and so on. Also on the click of a separate button with title "Submit" I want it to save which button was clicked out of the 1,2,3,4,5,6 buttons.

I am able to do it using 6 different actions for 6 Buttons but i need to use Collection outlet

Please Suggest

2

There are 2 best solutions below

0
V D Purohit On BEST ANSWER

Instead of using Collection of Outlet use one IBAction for all and just give different tag all six buttons create action like bellow:-

@IBAction func buttonOfOneToThree(_ sender: UIButton) {

    //MARK:- Below code print your selected button title and tag
    print(sender.tag)
    print(sender.titleLabel!.text!)
}

this will fulfil you requirement but if you want to learn collection outlet and action handling please refer this tutorial:-enter link description here

4
Anas Sabir On

you can use tags in order to determine the which buttons. Assign each button a tag lets say button 1 to 6 respectively tags 1 to 6 (you can set any) and depending upon your logic use them to identify.

in collection during cell for row initialize the tags for each button (i have seen this late)

hope it helps.

but why are using collection view do you have more than 6 buttons (is it dynamic) ?