I am intending to add a "blank" star icon on my page which will be changed to a "solid" star when a user clicks on it.
I tried to set the UIImage
as shown below:
UIImage *hotIcon = [UIImage imageNamed:@开发者_运维问答"blank_star.png"];
Can anyone advise me how I can use the addTarget:action:forControlEvents:
method? Can I even use this method on a UIImage
? Or must I definitely use a UIButton
?
You can add a method to action against a button like following:
[aButton addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
"addTarget: action:" and "removeTarget: action:" is for all UIControls (like UIButton). There is no way you can use it for UIImages directly.
There are multiple ways to achieve your requirement. (declare a custom UIControl and implement the way you want). But simplest approach is to use UIButton with required images (and background images).
精彩评论