Handle Clicks

PDF for offline use
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to write code that is triggered by a button ‘click’.

Recipe

There are a number of different ways that you can code against user-triggered events on a UIButton:

Anonymous Delegate

testButton1.TouchUpInside += delegate {
    new UIAlertView("Touch1", "TouchUpInside handled", null, "OK", null).Show();
};

Lambda Expression

testButton2.TouchUpInside += (sender, ea) => {
    new UIAlertView("Touch2", "TouchUpInside handled", null, "OK", null).Show();
};

Assign a delegate method

testButton3.TouchUpInside += HandleTouchUpInside;

and add the method:

void HandleTouchUpInside (object sender, EventArgs ea) {
    new UIAlertView("Touch3", "TouchUpInside handled", null, "OK", null).Show();
}

Make sure to clean up event handlers like this at some point in your code – either when the object has been disposed, or perhaps when the event has been raised. This can be done by declaring:

testButton3.TouchUpInside -= HandleTouchUpInside;

Additional Information

UIButton has a number of events that can be triggered by the user:

The TouchUpInside event occurs when the user performs a touch operation that begins and ends inside the bounds of the button.

The TouchUpOutside event, on the other hand, is triggered when the user begins a touch inside the button but lifts their finger outside its bounds.

The TouchDown event occurs as soon as a touch starts in the button, regardless of where the touch ends.

Generally the TouchUpInside event is used to respond to user interaction with a button.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.