So I created my custom button and set the image. I set the image for nonselectedImage and selectedImage above in load mode. When loading a view, an unselected image is displayed. When I click the button, the event is fired, but it cannot change the image set in the action method.
starImageBtn5 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
starImageBtn5.frame = CGRectMake(231.0, 67.0, 20.0, 20.0);
[starImageBtn5 setBackgroundImage:nonSelectedImage forState:UIControlStateNormal]
[cell.contentView addSubview:starImageBtn5]
[starImageBtn5 addTarget:self action:@selector(actionForStar5) forControlEvents:UIControlEventTouchUpInside]
[starImageBtn5 release];
This is the method when I try to change the image. StarOneSelected is a bool variable.
- (void) actionForStar1
{
if(starOneSelected)
{
UIImage *image =[UIImage imageNamed:@"star-selected.png"] ;
[starImageBtn1 setBackgroundImage:image forState:UIControlStateNormal];
starOneSelected = FALSE;
}
else
{
[starImageBtn1 setBackgroundImage:selectedImage forState:UIControlStateNormal];
starOneSelected = TRUE;
}
}
Please look at the code and try to help me out.i went through all the questions posted on this forum, as well as other forums, but could not find out where I am mistaken. Thanks in advance.
source
share