简体   繁体   中英

How to fire TouchUpInside of UIButton Programmatically in xamarin ios C#

I am trying to fire an touchupinside event of Uibutton programmatically. How can i do that ? The code I tried uses PerformSelector but i get this error

"Error MT4117: The registrar found a signature mismatch in the method
 'VCPAckage2.ActivityViewController.TouchUpInsideEvent' - the
 selector 'TouchUpInsideEvent:' indicates the method takes 1
 parameters, while the managed method has 2 parameters. "

I want to achieve something like

UIButton.FireEvent("TouchUpInsideEvent") - this will fire the TouchUpInsideEvent

UIButton.PerformSelector(new MonoTouch.ObjCRuntime.Selector ("TouchUpInsideEvent:"), null, 2000f);

Here's the code

 private void LoadFn()
 {
    UIButton btnSubmit = new UIButton(new RectangleF(0,0,View.Frame.Width,40));
    btnSubmit.TouchUpInside+=TouchUpInsideEvent;
 }   

 [Export("TouchUpInsideEvent:")]
 private void TouchUpInsideEvent(object sender,EventArgs e){
 float yy = AppConstants.ZeroVal;
 if (FeedbackSubmittedReturnFlag == true) {
        yy =  ChildScrollView2.Subviews[1].Frame.Height+ChildScrollView2.Subviews[1].Frame.Y;
 }
     this.ParentScrollView.SetContentOffset (new PointF (View.Frame.Width, yy), false);
 }

The following code snippet would be suffice

btnObj.SendActionForControlEvents(UIControlEvent.TouchUpInside);

It has to be called from Main thread

There's a few different things above.

First, the MT4117 is correct. It happens because your [Export] attribute specify a selector to a method that has only one parameter (ie it has only one : ) while the managed method has two parameters (which is the default for .NET events). The registrar will spot such conditions and report errors.

Second, the PerformSelector methods are bindings over performSelector:... selectors (most are defined on the NSObject protocol , not class). As such they have the same limitations (eg the number of arguments they can handle).

Third, there are several ways you could call your own code. An easy one would be, like @jonathanpeppers suggested, to directly call your managed method when needed.

Another one would be to adjust your code to match both 1 and 2 requirements, eg

// assign one (two parameters) method as a .NET event
btnSubmit.TouchUpInside += TouchUpInsideEvent;
...
// call another (one parameter) method like a selector
any_nsobject.PerformSelector (new Selector ("TouchUpInsideEvent:"), sender as NSObject, 0f);
...

// have the 2 parameters method call the(1 parameter) export'ed method
private void TouchUpInsideEvent (object sender, EventArgs e)
{
    TouchUpInsideEvent (sender as NSObject);
}

[Export ("TouchUpInsideEvent:")]
private void TouchUpInsideEvent (NSObject sender)
{
    Console.WriteLine ("yay!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM