articles

Home / DeveloperSection / Articles / Handling Button Clicks Events

Handling Button Clicks Events

Tarun Kumar4844 29-Nov-2015

Previously, we learn to how to set button in iOS app development: Buttons in iOS 

 Now, we will see how to write code that is triggered by a button click. We can handle click events in different ways at the time whenever user clicks on a UIButton.

Here we are describing some different ways to handle events like using anonymous delegate, using lambda expression, and some other ways.

Use following codes to handle events:

By using Anonymous delegate:
buttonObj1.TouchUpInside += delegate {
          new UIAlertView(“Clicked Button1”, “Using TouchUpInside”,
                                         null, “OK”, null).Show();
};

By using Lambda expression:

buttonObj2.TouchUpInside += (sender, ea) => {
         new UIAlertView(“Clicked Button2”, “Using TouchUpInside”,
                                           null, “OK”, null).Show();
};

By assigning a delegate method:

buttonObj3.TouchUpInside += HandleTouchUpInside;

 By adding a method:

void ButtonEventFired(object sender, EventArgs ea) {
         new UIAlertView(“Clicked Button3”, “Using TouchUpInside”,
                                           null, “OK”, null).Show();
}

We can use a different number of events fired by the user using UIButton.

Here are some of them:

TouchUpInside event : this event will occur when the user performs a touch operation which Begins & Ends inside the bounds of the UIButton.

TouchUpOutside event : this event will occur when the user Begins a touch inside the UIButton but lifts their finger outside its bounds.

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

(Mostly ‘TouchUPInside’ UIButton event is used for responding the user interation.) 

Next, we will learn how to set Image on UIButton on different states : Setting UIImage on a UIButton


Updated 17-Aug-2019

Leave Comment

Comments

Liked By