Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger an IBAction method programmatically vs. via a button, etc?

I can easily "do something", by creating an IBAction method, and connecting it to a button in IB. For example...

-(IBAction)popThat:(id)sndr{ [windy setFrame:eRect display:YES];}

However, I cannot, for the life of me, figure out how to do this via a simple, callable method... i.e.

-(void) popThatMethod { [windy setFrame:eRect display:YES]; }

-(void) awakeFromNib  { [self popThatMethod];               }

I would expect that this method would DO the same thing as clicking the button... as they are identical... but NO. Nothing happens. What am I missing here?

like image 787
Alex Gray Avatar asked Feb 09 '12 22:02

Alex Gray


2 Answers

I don't state that this is categorically the best answer, or the right way to do it, but one approach that works is to put the following in -applicationDidFinishLaunching::

[self performSelector: @selector(popWindow:) withObject:self afterDelay: 0.0];

This causes it to be called on the next pass of the runloop, by which time whatever was not in place before is now in place.

like image 52
ipmcc Avatar answered Nov 03 '22 00:11

ipmcc


Depending on what you're trying to do, you might want

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

Which triggers the button as if it had been touched, and forces whatever actions are connected to it.

NOTE: that's the answer from when I asked this SO question.

like image 43
Olie Avatar answered Nov 02 '22 23:11

Olie