Add / Remove EventHandler for UIBarButtonItem

In the constructor, you can define an EventHandler :

 UIBarButtonItem logoutButton = new UIBarButtonItem (UIBarButtonSystemItem.Stop, logoutButtonEventHandler); private void logoutButtonEventHandler(object sender, EventArgs args){ Console.WriteLine("Logout"); } 

Can I remove EventHandler subsequently? Perhaps not using the EventHandler at all, and instead use the Action / Target UIBarButtonItem ? I do not find examples. Only anonymous methods are used all the time.

How do you do this?

+4
source share
2 answers

Create an instance of the object, and then install the handler:

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

To remove it, use the syntax -= :

  logoutButton.Clicked -= logoutButtonEventHandler; 

Just be careful with commom traps when you do this because they can cause a memory leak.

+3
source

UIBarButtonItem has a clicked event so you can subscribe and unsubscribe from it.

+1
source

All Articles