iOS Attaching a Method to a Button


Example

To add a method to a button, first create an action method:

Objective-C

-(void) someButtonAction{
    NSLog(@"Button is tapped");

}

Swift

func someButtonAction() {
        print("Button is tapped")
    }

Now to add this action method to your button, you have to write following line of code:

Objective C

[yourButtonInstance addTarget:self action:@selector(someButtonAction) forControlEvents:UIControlEventTouchUpInside];

Swift

yourButtonInstance.addTarget(self, action: #selector(someButtonAction), forControlEvents: .touchUpInside)

For ControlEvents, all members of ENUM UIControlEvents are valid.