我正试图在我的Xamarin.iOS应用程序中注册一个单击事件。不过,我不想用这样的方法来注册事件。
clicks/
我想注册他们从故事板,似乎有功能来做到这一点。

在我的ViewController中,我有以下代码:
public void ButtonPressed(object sender, EventArgs ea)
{
UIAlertView error = new UIAlertView("Test Title", "This is a test", null, "Ok", null);
error.Show();
}当我运行应用程序并按下按钮时,我在appDelegate中得到一个错误
Foundation.MonoTouchException: Objective异常抛出。名称: NSInvalidArgumentException原因:-HomeViewController ButtonPressed::未识别的选择器发送到实例0x14691650
如何注册单击事件?
发布于 2015-08-20 21:48:37
查看您的设计器文件,看看您的ViewController,它会有如下的功能:
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 ()
{
}
}
}因此,只需像这样实现该方法:
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");
}
}
}发布于 2019-08-02 20:00:05
检查ViewController的分部类。它应该有这样的东西:
[Action ("ButtonPressed:")]
[GeneratedCode ("iOS Designer", "1.0")]
partial void ButtonPressed (UIButton sender);然后将partial放到您的方法中:
public partial void ButtonPressed(object sender, EventArgs ea) {
//...
}https://stackoverflow.com/questions/32128073
复制相似问题