开发者

Use addTarget:action:forControlEvents: method to change a UIImage?

开发者 https://www.devze.com 2023-03-10 17:28 出处:网络
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 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消