Search code examples
iosswift3uibutton

UIButton With Unique Identifier


Searched questions here This but not completely solving my problem.

Problem here is I have around 100 buttons in UIScrollView, and based on response from server I have to highlight buttons (i.e. Server sends me "Button_Unique_Id": "Button_ABC_1"). Now here the problem is how do I create a UIButton with unique identifier "Button_ABC_1", in such a way that when I get response from server, I can identify my button with the Button_Unique_Id value and do alterations just to that button.

button.tag = index

This approach fails as I can't identify button with value I got "Button_ABC_1"

I would appreciate response best practiced.


Solution

  • Keep references to your buttons in a Dictionary ([String : UIButton]) and use those strings as keys, then you can get the appropriate button by simply asking for it:

    let buttonOfInterest = buttons["Button_ABC_1"]