I want to kind of expand more on what was answered here already, Both responses are correct but i want to explain why/how this all works.
[button addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
The first thing to look at; The target
.
The target is the instance of a class, any class. The only requirement for this class is that it has to implement the action
.
action
is the method you wish to invoke when the user presses the button.
@selector(aMethod:)
Basically think of this as a method signature. Because Objective-c is a dynamic language, aMethod:
does not need to exist, but will crash your program if it does not.
So if we put this all together, Whenever I want to press this button:
The system will invoke the action
method, on the target
instance.
and for the method itself, it can look like this
- (void) aMethod:(id)sender { }