Xamarin.iOS – Handle UIButton Taps with C#

In this lesson

  • Use Debugger break points
  • Implement Lambda expressions to handle TouchUpInside event for button taps


Tap on time to skip ahead


How do we handle button clicks? We’re going to say testButton. There is no click property or event. Instead, you have TouchUpInside. To basically create an event handler, we hit += and this generates a lambda expression for us. I’m going to say Debug.WriteLine(“Pressed”) like this, now when I run this, when I hit button, we’ll see in our application how to put down pressed.


What’s all this stuff here? If I put a breakpoint in here, when I hit Goodbye, the app stops running and we go back to Xamarin Studio where I can start inspecting things and seeing what’s going on. If I double click on this and then right click on it, I can say Expression Evaluator. Basically, it will tell me what Sender is. As we can see, it’s a UIButton. Whenever it gets clicked it sends a reference to yourself in case you have to do some logic on it. You can see what our current title is, what the title color is, etc.


Another way you can see what’s going on is called Locals, the Locals tab here. If I increase this, you’ll see we have the event arguments that are being passed and there’s nothing to be passed with it. In this particular case, that’s not so useful.


That said, let’s actually have it do something with our UI. I’m going to say TestLabel.Text = Goodbye. Now, when we run this, it will change our UILabel. We can add some logic to this. If TestLabel.Text does not = Goodbye you do this, else you say Hello. As I toggle it, it will change.