问题
I'm making a button via code. I have the following line of code to trigger a method when the button is clicked:
[imagesButton addTarget:self action:@selector(photoClicked:) forControlEvents:UIControlEventTouchDown ];
The problem is that I can't pass data to the method through the @selector; If the button had a background image name "background.png" how would I go about sending the name of the background image to the method when the button is clicked? This all HAS to be through code.
Thanks!
-Shredder
回答1:
there must be a way to comment on an answer, but I don't know what it is. Anyway, Gobot above me forgot to write (id) before sender in the method declaration. Otherwise Gobot's example is ok.
回答2:
Well if you're trying to change a property of the button whose sending the message, your selector should have a parameter of sender, which is a pointer to the object that called it, which is your button in this case. For example:
- (void)photoClicked:(id)sender {
UIImage bg = [sender currentBackgroundImage]
}
来源:https://stackoverflow.com/questions/9918273/how-to-pass-data-through-selector-upon-custom-button-click