viewWithTag的作用就是根据tag属性获取到对应的view、imageview、label等等。
举例如下:
1.首先选两幅图作为正常状态和高亮状态的图片;正常为rose.png,高亮为pen.png
2.在xib文件上加上UIImageview控件,设置属性如下。注意设置tag属性
3.再加上一个UIButton控件,连接到操作btnClicked。xib文件最终如上图右边所示
4.添加代码;
在.h文件中
-(IBAction)btnClicked:(id)sender;
在.m文件中-(IBAction)btnClicked:(id)sender{
UIImageView *image=(UIImageView*)[self.view viewWithTag:12];
image.highlighted=YES;
}
5.运行程序可以发现,当点中button以后,图片就会切换为pen.png