4

One can define an EventHandler in the constructor:

UIBarButtonItem logoutButton = new UIBarButtonItem (UIBarButtonSystemItem.Stop, logoutButtonEventHandler);

private void logoutButtonEventHandler(object sender, EventArgs args){
    Console.WriteLine("Logout");
}

Is it possible to remove the EventHandler afterwards? Perhaps by not using an EventHandler at all and instead use the Action/Target properties of UIBarButtonItem? I don't find any examples. Only anonymous methods are used all the time.

How do you do that?

testing
  • 18,863
  • 44
  • 224
  • 396

2 Answers2

3

Instantiate your object and then set the handler:

var logoutButton = new UIBarButtonItem (UIBarButtonSystemItem.Stop)
logoutButton.Clicked += logoutButtonEventHandler;

To remove it afterwards use the -= syntax:

 logoutButton.Clicked -= logoutButtonEventHandler;

Just beware of commom pitfalls when you do so because they may cause memory leaks.

William Barbosa
  • 4,936
  • 2
  • 18
  • 36
1

UIBarButtonItem has Clicked event so you can subscribe and unsubscribe to it.

Giorgi
  • 29,755
  • 12
  • 86
  • 122