I found a property called imageForState in xcode but am having trouble getting it to do what I want. When a button is pressed, I want to execute a block of code depending on the button's image.
- (IBAction)favButton:(UIButton *)sender {
NSString *currentImage = [sender imageForState:UIControlStateNormal];
if([currentImage isEqualToString:#"already_fav"])
{
// execute code
}
}
However, I am getting the error:
Incompatible pointer types initializing NSString _strong with an expression type of UIImage
Can someone please tell how to get around this?
I am sure that you are not able to compare a string with an image. There is a solution however. All you have to do is set the tag for the image you are wanting to compare, and set the tag of the image you are comparing to.
image1.tag = 1;
image.tag =1;
if(image1.tag == image.tag) {
// execute code
}
that should help, and I hope that it does.
SO, for this exercise, i'll show you. Change the NSString to a UIImage
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
currentImage.tag = 1;
wantedImage.tag = 1;
if(currentImage.tag == wantedImage.tag) {
// do something
}
hopefully this helps you out :)
When you create a UIImage it is just an image and has no string value attached. The name is not carried by the UIImage object.
If it were me (and this isn't the only solution, just another suggestion), i'd create individual UIImage objects (depending on how many we're talking about here) as this should checkable while strings are not.
For example:
UIImage *image1 = [UIImage imageNamed:#"randomName"];
UIImage *image2 = [UIImage imageNamed:#"anotherRandomName"];
[myButton setImage:image2 forState:UIControlStateNormal];
if ([myButton imageForState:UIControlStateNormal] == image1) {
NSLog(#"The button shows image 1 for normal state");
}
else if ([myButton imageForState:UIControlStateNormal] == image2) {
NSLog(#"The button shows image 2 for normal state");
}
else {
NSLog(#"Error!!!!!!! :D");
}
you cant save the Image as a String
try
UIImage *currentImage = [sender imageForState:UIControlStateNormal];
than you can check:
if(currentImage == [UIImage imageNamed:#"already_fav"])
I did this for switching sound/noSound image on button:
- (IBAction)soundAction:(id)sender {
UIImage *sound = [UIImage imageNamed:#"sound"];
UIImage *noSound = [UIImage imageNamed:#"noSound"];
if ([[sender currentImage] isEqual:sound]) {
NSLog(#"IF SOUND IMAGE");
[sender setImage:noSound forState:UIControlStateNormal];
//your code
} else if([[sender currentImage] isEqual:noSound]) {
NSLog(#"IF NO SOUND IMAGE");
[sender setImage:sound forState:UIControlStateNormal];
//your code
}
}
Related
I am working with the tableview and Sqlite when i update my Table based on the values i need to change the image which is in tableview cell it is also working fine but when i scroll then my images are changing below is my code
Sqlite Table Update
[DBObject upDateTable:#"Green" :messageID];
How i am fetching values from my array
NSString *messageStatus = [[messageArray1 objectAtIndex:index] ColorStatus];
now I am checking colorStatus like below in cellForRowAtIndexPath
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
my images are assigning perfectly but when i scroll then it is changing i don't know where i am missing please help me out
cell.playButtonImageView.image=nil; // write this line before set image
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
if ([messageStatus isEqualToString:#"Green"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Green.png"];
[[ASNGlobalClass shareManager] setIsDelivered:YES];
}else if ([messageStatus isEqualToString:#"Red"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-Red.png"];
}else if ([messageStatus isEqualToString:#"Yellow"]) {
cell.playButtonImageView.image= [UIImage imageNamed:#"message-yellow.png"];
}
else
cell.playButtonImageView.image= [UIImage imageNamed:#"some-default-image-placeholder.png"]; // some-default-image-placeholder.png is an placeholder image name
I am trying to comparing image with button images but my code is not working for this requirement. What did I do here wrong? Please help me.
NSString *btnImage;
UIButton * sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([sharebutton.imageView.image isEqual:[UIImage imageNamed:#"arrow.png"]])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}
you can check image for specific state
if([[sharebutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"arrow.png"]])
{
//Image found
}
Maybe this will help you.
if (sharebutton.currentImage == UIImage(named: "arrow.png")) {
}
U will need to convert this into obj-c but i think u will get it. :)
You could use the following code:
if ([UIImagePNGRepresentation(firstImage) isEqualToData:UIImagePNGRepresentation(secondImage)])
If you want to compare button image with another image name then set your button image this way:
sharebutton.setImage(UIImage(named: "arrow.png"), forState: UIControlState.Normal)
Remember one thing here don't set background image for button.
Now you can compare button image with another image this way:
if (sharebutton.currentImage?.isEqual(UIImage(named: "arrow.png")) != nil) {
println("YES")
} else {
println("NO")
}
UIImage *image = [sharebutton imageForState:UIControlStateNormal];
if ([image isEqual:[UIImage imageNamed:#"arrow.png"]]) {
}
Do this,you can solve it.
This is what i'm using:
NSData *leftImageData = UIImagePNGRepresentation([sharebutton imageForState:UIControlStateNormal]);
//take note that using `imageForState:` only works if you set your image using `setImage:forState:`
//if you are using `setBackgroundImage:forState:`, `[shareButton currentBackgroundImage]` is what you need.
NSData *rightImageData = UIImagePNGRepresentation([UIImage imageNamed:#"arrow.png"]);
if ([leftImageData isEqualToData:rightImageData])
{
//same
}
else
{
//not same
}
For performance reasons I'd compare the file names rather than creating a temporary image which is much more expensive
NSString *btnImage;
UIButton *sharebutton;
btnImage = #"arrow.png";
[sharebutton setImage:[UIImage imageNamed: btnImage] forState:UIControlStateNormal];
if ([btnImage isEqualToString:#"arrow.png"])
{
NSLog(#"ok");
}
else
{
NSLog(#"else");
}
Of course this requires to maintain the variable btnImage to hold always the current file name
Try this:
UIButton *sharebutton;
UIImage *image1 = [UIImage imageNamed:#“arrow.png"];
[sharebutton setImage:image1 forState:UIControlStateNormal];
if (sharebutton.currentImage == [UIImage imageNamed:#"arrow.png"])
{
//do something here
}
I am creating an app in which I have used user registration.In registration page, I have created many UITextFields.I wants that when a user select any TextField then its background Image is change. I uses the code to do that is:
- (IBAction)touchBegin:(id)sender
{
//UILabel *opt = (UILabel *)[cell viewWithTag:101];
if (_text1.isSelected) {
_text1.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text2.isSelected){
_text2.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text3.isSelected){
_text3.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text4.isSelected){
_text4.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else{
}
}
But I am not getting the result according to my need.
What should I use to get correct output?
be sure whether your textfield border style is UITextBorderStyleNone.
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"image.png"];
I think that it's better and easier to use UITextFieldDelegate methods like this, for example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"big_star_non_rating"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"AnImageForNormalStateOrSetNilBackground"];
return YES;
}
Hope it helps.
I have a UIImageView that when a function called I want to change to a different ("active") image and when another called change back to the image before. This is the code:
- (NavButton *)initWithFrame:(CGRect *)fr andImage:(UIImage *)img andActiveImage:(UIImage *)acImg {
NavButton *a = [[NavButton alloc] initWithFrame:*fr];
[a setBackgroundColor:[UIColor clearColor]];
UIImageView *aImg = [[UIImageView alloc] initWithFrame:CGRectMake(8.5, 8.5, 28, 28)];
aImg.tag = 13;
aImg.image = img;
self.orginalImage = img;
self.activeImage = acImg;
[a addSubview:aImg];
return a;
}
- (void)setIsActive:(NSNumber *)isActive {
self.active = isActive;
if ([isActive isEqualToValue:[NSNumber numberWithBool:NO]]) {
[self undoActive];
} else {
[self redoActive];
}
}
- (void)undoActive {
UIImageView *a = (UIImageView *)[self viewWithTag:13];
a.image = self.orginalImage;
}
- (void)redoActive {
UIImageView *a = (UIImageView *)[self viewWithTag:13];
a.image = self.activeImage;
}
When I call [btn setIsActive:[NSNumber numberWithBool:YES]]; or [btn setIsActive:[NSNumber numberWithBool:NO]]; both times it removes the image, but when I don't call either the image stays there. So, how do I make it so when I call them it changes the images of the button to the correct image?
Instead of repeatedly assigning image to imageview, you can assign two images to the image view: one to "image" property and other to "highlightedImage" property. When you want to switch between the images, set the Boolean property "highlighted" as YES or NO.
It will be easier just to do your check as:
if (![isActive boolValue]) {
Then, do some debugging, add some breakpoints and / or logging. Check what values are actually being received. Are the flags set correctly. Are the images set correctly. Is anything nil.
I have this simple function where i sap rate the button animation and then start animation for every button but i dont know why only one button animate not others which clicked, please help me with this
- (IBAction)startAnimation:(UIButton *)button {
NSMutableArray* imagesArray = [[NSMutableArray alloc] init];
for (int images = 0; images < 15; images++) {
UIImage* buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"aaqqq00%02d.png", images]];
[imagesArray addObject:buttonImage];
}
NSArray* reversedAnim = [[imagesArray reverseObjectEnumerator] allObjects];
int buttonTag = button.tag;
animButton.adjustsImageWhenHighlighted = NO;
for (int images = 0; images < 15; images++) {
UIButton *animButton = (UIButton *)[self.view viewWithTag:buttonTag];
if (images <= buttonTag) {
animButton.imageView.animationImages = imagesArray;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0014.png"] forState:UIControlStateNormal];
} else {
animButton.imageView.animationImages = reversedAnim;
[animButton setImage:
[UIImage imageNamed:#"aaqqq0000.png"] forState:UIControlStateSelected];
}
NSLog(#"%#", animButton.imageView.animationImages);
animButton.imageView.animationDuration = 1; //whatever you want (in seconds)
animButton.imageView.animationRepeatCount = 1;
[animButton.imageView startAnimating];
}
}
You're passing the button, so doing the tag lookup seem pointless and error prone (if there are multiple views with the same tag in the hierarchy).
Setting the buttons image view animation images (animButton.imageView.animationImages) and then after that setting a single image ([animButton setImage:...) is also pointless.
What does your log statement say about the animation images?