How to transmit data via @selector when a user button is clicked?

I am making a button using code. I have the following line of code to run the method when the button is clicked:

[imagesButton addTarget:self action:@selector(photoClicked:) forControlEvents:UIControlEventTouchDown ]; 

The problem is that I cannot pass data to the method via @selector; If the button had a background image named "background.png", how can I send the name of the background image to the method when the button is clicked? All this should be through code.

Thanks!

-Shredder

+4
source share
2 answers

Well, if you are trying to change the property of a button by sending a message, your selector should have a sender parameter, which is a pointer to the object it calls, which is your button in this case, For example:

 - (void)photoClicked:(id)sender { UIImage bg = [sender currentBackgroundImage] } 
+1
source

there should be a way to comment on the answer, but I don't know what it is. In any case, the Gobot above me forgot to write (id) before the sender in the method declaration. Otherwise, the Gobot example is fine.

+1
source

Source: https://habr.com/ru/post/1404036/


All Articles