Add/remove EventHandler for UIBarButtonItem

耗尽温柔 提交于 2020-01-24 05:49:05

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/32097795/add-remove-eventhandler-for-uibarbuttonitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!