Click Xamarin Handler

I am trying to register a click event in a Xamarin.iOS application. I do not want to log events in a method like this.

http://developer.xamarin.com/recipes/ios/standard_controls/buttons/handle_clicks/

I want to register them from the storyboard, and there seems to be functionality for this.

Attempting to bind an event to a method in the controller

and in my ViewController I have the following code:

public void ButtonPressed(object sender, EventArgs ea) { UIAlertView error = new UIAlertView("Test Title", "This is a test", null, "Ok", null); error.Show(); } 

When I launch the application and click the button, I get an error message in appDelegate

Foundation.MonoTouchException: Objective-C exception. Name: NSInvalidArgumentException Reason: - [HomeViewController ButtonPressed:]: unrecognized selector sent to instance 0x14691650

How to register click event?

+4
source share
1 answer

Check your designer file for your ViewController, it will have something like this:

 using Foundation; using System; using System.CodeDom.Compiler; using UIKit; namespace TEST { [Register ("ViewController")] partial class ViewController { [Action ("ButtonPressed:")] [GeneratedCode ("iOS Designer", "1.0")] partial void ButtonPressed (UIButton sender); void ReleaseDesignerOutlets () { } } } 

So, just implement this method as follows:

 using System; using UIKit; namespace TEST { public partial class ViewController : UIViewController { public ViewController (IntPtr handle) : base (handle) { } public override void ViewDidLoad () { base.ViewDidLoad (); } partial void ButtonPressed (UIButton sender) { Console.Write ("HEY mark this answer as accepted"); } } } 
+3
source

All Articles