126

Is there a way to programmatically fire a button click event? I have a button placed there in an UIView, and in a particular scenario i want to click the button via code, not manually as a user. Is it possible in iOS development? Please provide your suggestions and guide me how to do that.

Thanks.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
Getsy
  • 4,887
  • 16
  • 78
  • 139

5 Answers5

317

Sort of like Ken's answer, but more flexible as it'll keep track of the buttons actual actions if you change them or add more than one.

[button sendActionsForControlEvents:UIControlEventTouchUpInside];
Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • 8
    Does this highlight the button if `button.showsTouchWhenHighlighted = YES` like it would with an actual touch? – chown Sep 10 '12 at 18:11
  • 2
    This answer is great b/c it can easily be translated to other controls such as UISwitch (UIControlEventValueChanged) and in my case is much better than calling the target/action code directly b/c I want the switch to switch! Thx. – John Erck Oct 27 '13 at 15:14
  • No it does not highlight the button if button.showsTouchWhenHighlighted = YES. ;( – Richie Hyatt Mar 04 '14 at 16:46
  • 7
    if you need the highlight you can do this. [button setHighlighted:YES]; [button sendActionsForControlEvents:UIControlEventTouchUpInside]; [button setHighlighted:NO]; and it works great. -rrh – Richie Hyatt Mar 04 '14 at 16:54
  • Hello @zaky, if I want only animation, without send action, how can I do it? – benhi Mar 26 '15 at 11:02
  • This method successfully triggers the action, but not the click effect. Please see https://stackoverflow.com/questions/7719412/how-to-ignore-touch-events-and-pass-them-to-another-subviews-uicontrol-objects for a better and working method to get the click effect as well. – Abu Jul 13 '17 at 09:12
13

I had the same issue as above. I would have posted a comment, but my reputation isn't high enough. So, I will instead post the full answer:

[btn setHighlighted:YES];
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
[btn performSelector:@selector(setHighlighted:) withObject:NO afterDelay:0.25];

This will programmatically hit the button and highlight it for a seemingly normal amount of time. Richie's suggestion didn't work as the button was only highlighted (if at all) for an imperceptible amount of time.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Chad Hine
  • 191
  • 2
  • 3
  • It wasn't highlighted, drawing only occurs after the current chunk of code is finished executing. This is a nice fix. – mattsven Jun 21 '14 at 22:35
8

Why not just execute the code that executes when the user presses the button?

-(void)myMethod
{
   // do stuff
}

-(IBAction)myButtonClick:(id)sender
{
    [self myMethod];
}

-(void)clickMyButton
{
    [self myMethod];
    // OR
    [self myButtonClick:nil];
}
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • No, my requirement is not calling the method, how to fire button click event programmatically, not by when user click it manually. I created a UIButton programmatically in an UIView, i want to fire button click event automatically after a certain time interval via program itself, not by the user. – Getsy Apr 11 '11 at 18:29
  • Then it is not possible without synthesizing the touch. See http://cocoawithlove.com/2008/10/synthesizing-touch-event-on-iphone.html. However, you cannot submit your app to the App Store using this method, as it uses a private API. It is normally used for debugging purposes. – Evan Mulawski Apr 11 '11 at 18:32
  • You don't explain why you need to simulate a touch vs just calling the method but that is by far the easiest way to go. Your timer action can call any method you want and the method it calls can give feedback in the UI that the button is being pressed by changing the button state (selected, highlighted, etc). If that won't work, so be it. – XJones Apr 11 '11 at 19:19
1

Yes,

[_button setHighlighted:YES];
[_button sendActionsForControlEvents:UIControlEventTouchUpInside];
[_button setHighlighted:NO];

But the problem here is you will not be able to see those event because its too fast . so what you can do is try the above code with NSTimer .

NSTimer *quick = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(MeFirst:) userInfo:nil repeats:NO];

MeFirst is the method i have created.

Bharat jain
  • 419
  • 1
  • 9
  • 16
1

If you're talking about a UIButton, you can just call the same method that the UIButton is calling when it is tapped. For example:

[self myButtonsTargetMethod];
Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
  • No, my requirement is not calling the method, how to fire button click event programmatically, not by when user click it manually. I created a UIButton programmatically in an UIView, i want to fire button click event automatically after a certain time interval via program itself, not by user. – Getsy Apr 11 '11 at 18:28