温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - How to have access to a button that's outside of my current UIView? UIKIT ; Xamarin.ios native;
xamarin.ios uikit

其他 - 如何访问当前UIView之外的按钮?

发布于 2020-03-31 23:24:59

对于我的愚蠢问题,我感到抱歉,但是我有一个UIView类,它具有一个将更改ViewController中数据的按钮。如何访问该类中的单击按钮,以便可以更改ViewController中的数据?

谢谢您的宝贵时间。

查看更多

提问者
memyselfandi
被浏览
27
Lucas Zhang - MSFT 2020-02-03 09:25

您可以在UIView中定义一个EventHandler,并在单击按钮时在ViewController中调用它。

在你的UIView中

public event EventHandler ButtonClickEvent;

//...
button.TouchUpInside += Button_TouchUpInside;

private void Button_TouchUpInside(object sender, EventArgs e)
{
   EventHandler handler = ButtonClickEvent;
   handler?.Invoke(sender, e);
}

在ViewController中

yourUIView.ButtonClickEvent += ButtonClickHandler;
private void ButtonClickHandler(object sender, EventArgs e)
{
   // change the data here
}