Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically fire button click event?

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];

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.


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];
}

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];