Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton created programmatically. How do I send touch up's to an IBAction?

I have a UIButton which is created programmatically. How do I get this UIButton to send its touch up events to an IBAction I have?

like image 489
Andrew Avatar asked Mar 11 '11 21:03

Andrew


3 Answers

addTarget:action:forControlEvents: method.

[myButton addTarget:myClass action:@selector(myEventHandler) forControlEvents: UIControlEventTouchUpInside];

From http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html.

like image 170
GendoIkari Avatar answered Oct 03 '22 00:10

GendoIkari


with this line of code you will fire UIbutton action by programmatically

[button sendActionsForControlEvents:UIControlEventTouchUpInside];

For swift

button.addTarget(self, action:#selector(functionNameHere), for: UIControl.Event.touchUpInside)
like image 25
Waseem Shah Avatar answered Oct 03 '22 02:10

Waseem Shah


[button addTarget:self 
           action:@selector(myMethod:)
 forControlEvents:UIControlEventTouchUpInside];

Like magic =)

like image 33
ultifinitus Avatar answered Oct 03 '22 00:10

ultifinitus