Warm tip: This article is reproduced from stackoverflow.com, please click
xamarin.ios uikit

How to have access to a button that's outside of my current UIView? UIKIT ; Xamarin.ios native;

发布于 2020-03-31 22:57:01

I am sorry for my dumb question but I have a class that is an UIView and it has a button that will change the data in my ViewController. How can I access the click button that is in that class so the data in my ViewController can change?

Thank you for your time guys.

Questioner
memyselfandi
Viewed
34
Lucas Zhang - MSFT 2020-02-03 09:25

You could define an EventHandler in your UIView and invoked in ViewController when you clikc the button .

in your UIView

public event EventHandler ButtonClickEvent;

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

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

in ViewController

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