Add Longpress gesture and Click event on UIButton - iphone

I am new to iPhone.
I want to add both a long press gesture and a click event to my Button, is this possible?
When I add both events to the button and then long press, my click event gets fired (on click I navigate to a new page), but my long press event never gets fired.
Here is my code snippet:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(xpos, ypos, 120,130);
[button setBackgroundImage:[UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"32"]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
LongPress = [[UILongPressGestureRecognizer alloc] init];
[LongPress addTarget:self action:#selector(longPressDetected:)];
LongPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[button addGestureRecognizer:LongPress];
[LongPress release];
[self.view addSubview:button];
How do I add both events?
Any help will be appreciated.

Change the line as follows,
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
You click event will be fired as soon you touch the button if you use touchdown event. TouchupInside fires the action on touchup.
To get title ,
- (void)longPressDetected:(UIGestureRecognizer *)gestureRecognizer
{
UIButton *button = (UIButton *)[gestureRecognizer view];
NSLog(#"%#",[button titleForState:UIControlStateNormal]);
}

Related

Overwriting an image button with another image when I click that button

I have created an image button (a.png) infront of each row in the cellForRow method. In the same method itself I'm calling a buttonPressed method which has the code to be executed when the button is pressed. And I want that on the pressing of the button, the button image should be changed to another image (b.png) of the same size.
Please help with how to do that.
[but setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateSelected];
try this......
Create Custom Cell for UITableView and UIButton on Custom Cell.
Create property for UIButtion.
In cellForRowAtIndexPath: method
//Set Action on Button in Table View Row
[cell.UIButtonOutlet addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
This will create action from your button.
And in method
-(void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches=[event allTouches];
UITouch *touch=[touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];
//GET IndexPath
NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];
selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];
[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Try the following method
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button setFrame:CGRectMake(12,12,200,200)];
[button addTarget:self action:#selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
then in the click action method of button
-(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
[self yourFunction:(UIButton *)button];
}
then finally
-(void)yourFunction:(UIButton *)button{
button.selected = ! button.selected;
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
}
}
Try this when initializing your button
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"a.png"] forState:UIControlStateNormal];
Then modify your method(which is called when button is pressed) like this
-(void)yourMethod:(id)sender{
UIButton *btn=(UIButton*)sender;
[btn setImage:[UIImage imageNamed:#"b.png"] forState:UIControlStateNormal];
//your other code
}

UIButton : Background Image setting Issue when button state is highlighted?

I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected

How to identify which button is clicked in Objective-C?

I am a new iPad developer.
I have created UIButton programmatically, in which I want to identify user has clicked on which button and according to that I want to do some action.
How should I identify this?
Here is my code snippet:
for(int i=0;i<[_array count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=count;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor=[UIColor redColor];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(xpos, 270.0, 200.0, 150.0);
[self.view addSubview:button];
}
This is what I'm doing now: I thought I will assign count to each button and I will pass this count to button click method. Is this a good idea? Is there any other way possible?
On button click I'm calling aMethod:
-(void)aMethod:(id)sender{
NSLog(#"btn clicked");
}
Any help will be appreciated!
-(void)aMethod:(UIButton*)sender{
NSLog(#"btn clicked %d", sender.tag);
}

how to change button image in tableview when that button clicked

i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states

UIControl - changing assigned selectors: addTarget & removeTarget

I'm using 10 buttons in my interface and need, from time to time, to change the button's selector.
Am I required to use:
-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
before I change the selector or can I just use:
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
I'm concerned that if I change the selector using the addTarget: method sans the removeTarget: method that I'll essentially "stack up" selectors for my UIButton to fire when it is pressed.
Yes you should always remove the previously add target before assigning the new target to the button. Like this---
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 200, 50)];
[btn setTag:101];
[btn addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
btn = (UIButton *)[self.view viewWithTag:101];
[btn removeTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
now if you do this
btn = (UIButton *)[self.view viewWithTag:101];
[btn addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
then both the methods method1 and method2 will be called.
Hope this helps.
Yes, you will need to remove the old target/action or both the old and new actions will be performed.