3

I just need to trigger a UIButton programmatically . I have seen many of posts such as http://stackoverflow.com/questions/5625651/programmatically-fire-button-click-event

[btnLocations sendActionsForControlEvents:UIControlEventTouchDown];

This statement working fine in the " viewDidLoad " and also in another button handler.

-(void)viewDidLoad
{
[btnName2 sendActionsForControlEvents:UIControlEventTouchDown];
}

&&&&&&&

- (IBAction)btnName1:(id)sender {
[btnName2 sendActionsForControlEvents:UIControlEventTouchDown];
}

This works fine . but why it is not triggering from user defined method. Like this :

-(void) myMEthod
{
  if(true)
{
 [btnName2 sendActionsForControlEvents:UIControlEventTouchDown];
}
}

Simple one . But I didn't get the reason

I just need to know why it is not happening ?? And any alternative to achieve it?

Thanks.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Kumar KL
  • 15,315
  • 9
  • 38
  • 60

1 Answers1

3

Could be an issue trying to invoke a UI event on another thread. Try adding this:

    - (void)fireButtonEvent:(id)unused
    {
            [btnName2 sendActionsForControlEvents:UIControlEventTouchDown];
    }

and then call using

    [self performSelectorOnMainThread:@selector(fireButtonEvent:)
          withObject:nil
          waitUntilDone:false];
Mike M
  • 4,358
  • 1
  • 28
  • 48
  • Works fine . Thanks but Not included all functionalities . Here adding – Kumar KL May 02 '13 at 12:10
  • Its triggering for some Logs , Actually I'm adding subView to the View . but It is not happening from this way . Happening perfectly from UI Click – Kumar KL May 02 '13 at 12:14