UIButton UIControlEventTouchUpInside does not fire, but UIControlEventTouchDown does fire - objective-c

I am implementing UIButton and my UIControlEventTouchUpInside event does not fire, even though UIControlEventTouchDown does fire.
UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePathImage = #"img.png";
NSString *filePathImageTap = #"img-tap.png";
UIImage *buttonImage = [UIImage imageWithContentsOfFile:filePathImage];
UIImage *buttonImageTap = [UIImage imageWithContentsOfFile:filePathImageTap];
[btnClose setImage:buttonImage forState:UIControlStateNormal];
[btnClose setImage:buttonImageTap forState:UIControlStateSelected];
[btnClose setImage:buttonImageTap forState:UIControlStateHighlighted];
[btnClose addTarget:self action:#selector(close:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClose];

I have an assumption why it doesn't take the UIControlEventTouchUpInside:
I had two "BannerView" (Advertisement) in two different ViewControllers. In one it just worked fine in the other I saw how the UIButton was touched and the second image (darker) appeared. But the selector was not hit. Then I realized, that the Up-Event was absorbt by a UISwipeGestureRecognizer from a UIImageView below. Some Recognizer compete against each other. I searched for an overview to see witch ones these are, but found nothing.
Another solution:
If you see that the ButtonState doesn't change u have to look if you have a View over your Button.

Related

setting background on UIButton on Map Callout

Hi I am trying to set a background image on my detail disclosure UIButton attached to my MapView MKAnnotation callout view. Checking on stackoverflow there are consistent examples of how to set this, however it doesn't seem to work for my callout UIButton. My question is, does this not work, simply because of the mapview or am I missing something else?
Here is my code:
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"Tch4"];
//pinView.calloutOffset = CGPointMake(0, 32);
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImage *btnImage = [UIImage imageNamed:#"4stars"];
[pinView.rightCalloutAccessoryView setBackgroundImage:btnImage forState:UIControlStateNormal];
I receive the error on the final line. Error is: No Visable interface for UIView declares the selector 'setbackgroundimage: forstate'
Thanks for your help with this
Your problem is rightCalloutAccessoryView returns a UIView instance. And UIView does not respond to setBackgroundImage:forState: method.
What you are looking for is to add a UIButton instead something like this:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(0, 0, 32, 32); // Set your own frame
[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[infoButton setBackgroundImage:btnImage forState:UIControlStateSelected];
pinView.rightCalloutAccessoryView = infoButton;
you cant set background image if your typebutton DetailDisclosure.
you can addsubview image to rightCalloutAccessoryView but without button..
so if you still use typebutton DetailDisclosure you cant set background

Shows Touch On Highlight for UIImage

The UIButton has a cool attribute that is called "Shows Touch On Highlight" that allows me to tell the user "you touched this button".
Is there any way I can implement this for a UIImage? I want the user to see the point touched inside the UIImage.
If you are using a little trick. make a transparent button above UIImageView. so must set a UIImageView userInteractionEnabled true bec default false. as follows:
imageView.userInteractionEnabled = YES;
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = imageView.frame;
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setBackgroundColor:[UIColor clearColor]];
[imageView addSubView:clearButton];
other trick, create a transparent button, and set image your want.
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
clearButton.showsTouchWhenHighlighted = YES;
[clearButton setImage:[UIImage imageNamed:#"your_image"] forState:UIControlStateNormal];
[clearButton setBackgroundColor:[UIColor clearColor]];
No, it doesn't in same way as in UIButton, but you can use UIButton of type custom, instead to solve it for you.
Another help with UIImageView is setting it's Highlight image, but still you have to work for effect.

change the background image of button in objective-c?

I've a UIButton on cell of UITableView in my VC like this
arrowBtnUp = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtnUp.frame = CGRectMake(50,(65-19)/2,31, 31);
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[arrowBtnUp addTarget:self action:#selector(slideAction) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:arrowBtnUp];
and this my slideAction
-(void) slideAction
{
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
// also tried with UIControlStateSelected/Highlighted
}
But it didn't work.I found this link but didn't help.
Any suggestion or sample would be appreciated.
Your code should work.Anyways change your code like this and try
-(void) slideAction:(id)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
and
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
and ensure that you have arrow_down.png in your app bundle
The issue is you are reusing the arrowBtnUp instance of UIButton.
Hence in the slideAction method you won't get the pressed buttons reference.
For getting the reference in the slideAction method you need to pass it as an argument.
So change the code like:
[arrowBtnUp addTarget:self action:#selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) slideAction:(UIButton *)myButton
{
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateNormal];
}
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
Background image is different than the icon on the button. Try this instead:
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateNormal];
If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_up.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"arrow_down.png"] forState:UIControlStateSelected];
There is no need to manually switch the background images in your slideAction: target method.

UIButton tap not responding

I added a new UIButton
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setFrame:CGRectMake(x, y, width, height)];
[newButton setTitle:#"" forState:UIControlStateNormal];
[newButton setTag:x+INDEX_OFFSET];
[newButton setBackgroundImage:image forState:UIControlStateNormal];
[newButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:newButton];
[self bringSubviewToFront:newButton];
and I can see it on the screen but it is not responding to a tap gesture. It should call "buttonPressed". Any help would be super!
One of the possible problem is that you are adding the button in a frame position out its superview. Just to check it easily try to set clipToBounds property to YES in its superview bounds, then run the app. If you don't see the button it means that you set the button position out superivew position, that's why it doesn't respond to touches.
Another possible problem is if you add the UIButton as a subview to an UIImageView (which you shouldn't, but it works). In this case, you could either add it to a UIView or set UIImageView's userInteractionEnabled property to YES.

add label to UIButtons in iCarousel

I implemented iCarousel method in my ViewController and all is working properly but there is one thing, i want to add below the carousel view label and for each image, i want to change the text in this label area. Here is my code I used for creating each image as button.
Thanks in advance.
UIImage *buttonImage =[NSArray arrayWithObjects:[UIImage imageNamed:#"Hotels.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"FastFood.png"],
[UIImage imageNamed:#"Taxi.png"],nil];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:[buttonImage objectAtIndex:index]forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
iCarousel has a delegate method:
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel;
Use that method to update your label as it will fire every time the centred item in the carousel updates.
To actually work out which label text to use, you probably want to create a second array of strings to match your array of images. That way you can use the carousel.currentItemIndex property as the index into the strings array to select the correct label.