I'm attempting to fake the act of clicking a button, by programmatically calling it within my ViewWillAppear() function.
The onclick function is defined in my ViewDidLoad(), and you can see I am trying to use a Perform Selector to manually call the button.
The button does not appear to be running. Any ideas?
public override void ViewDidLoad()
{
base.ViewDidLoad();
idButtonScanLoad.TouchUpInside += async (sender, ea) =>
{
System.Console.WriteLine("Scan button pressed");
};
}
[Export("TouchUpInsideEvent:")]
private void TouchUpInsideEvent(NSObject sender)
{
Console.WriteLine("yay!");
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.PerformSelector(new ObjCRuntime.Selector("TouchUpInsideEvent:"), this as NSObject, 10f);
}
Here is the answer, you should call it from your ViewDidLoad method.
myButton.SendActionForControlEvents (UIControlEvent.TouchUpInside);
If you want to create a button's click event, you can use the this
public override void ViewDidLoad()
{
Mybutton.AddTarget(ButtonEventHandler, UIControlEvent.TouchUpInside);
}
public void ButtonEventHandler(object sender, EventArgs e)
{
if(sender==Mybutton)
{
//do stuff here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With