how to pass data through @selector upon custom button click?

試著忘記壹切 提交于 2019-12-06 04:38:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!