Kumar KL
Kumar KL

Reputation: 15335

Not Triggering event handler of UIButton from a Method

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.

Upvotes: 3

Views: 1245

Answers (1)

Mike M
Mike M

Reputation: 4436

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

Upvotes: 3

Related Questions