简体   繁体   English

如何从事件中删除/取消注册事件处理程序?

[英]How do I remove/unregister event handler from an event?

I've made a custom uitableviewcell . 我做了一个定制的uitableviewcell。 The cell consist out of labels and a UIButton. 单元格由标签和UIButton组成。 Everything was working fine until I found out that there is a small problem with the reusable cell in combination with adding an event to an uibutton. 一切都工作正常,直到我发现可重复使用的细胞与向uibutton添加事件相结合存在一个小问题。 So the first time the cell works great when I click the uibutton. 所以当我点击uibutton时,单元格第一次工作得很好。 But the moment I scroll it starts giving me the wrong detail info. 但是当我滚动它的那一刻开始给我错误的细节信息。 If I comment out the reusable cell part (and let it make a new cell everytime) it just works fine with no problems. 如果我注释掉可重复使用的单元格部分(并且每次都让它创建一个新的单元格),它就可以正常工作而没有任何问题。 So my assumptions is that the event 'hangs on' into cell with the old info. 所以我的假设是事件'挂起'进入具有旧信息的单元格。 I add the event like this: 我添加这样的事件:

myButton.TouchDown += delegate{
  //some code here
}

Obviously I cannot unregister it like this right? 显然我不能像这样取消注册吗? What is the easiest way to overcome this particular problem in monotouch? 在monotouch中克服这个特殊问题最简单的方法是什么?

You can have anonymous methods and lambdas as event handlers. 您可以将匿名方法和lambda作为事件处理程序。 Just save them as local variables. 只需将它们保存为局部变量即可

EventHandler theHandler = delegate(obj sender, EventArgs e) 
{ Console.WriteLine("The handler!"); };
//... or: EventHandler theHandler = (sender, args) => { Console.WriteLine("The handler!"); };
private void HookEvent()
{
    myButton.TouchDown += this.theHandler;
}

private void UnhookEvent()
{
    myButton.TouchDown -= this.theHandler;
}

If you want to be able to unregister your event handler, you shouldn't use anonymous methods (or lambdas). 如果您希望能够取消注册事件处理程序,则不应使用匿名方法(或lambdas)。 Move your code into an actual method and register it like this: 将您的代码移动到一个实际的方法,并将其注册如下:

private void MyMethod()
{
  //some code here
}

// register
myButton.TouchDown += MyMethod;

// unregister
myButton.TouchDown -= MyMethod;

OK. 好。 I've been trying a lot of stuff to solve this but ChrisNTR came with the right solution to my particular problem. 我一直在尝试很多东西来解决这个问题,但ChrisNTR为我的特定问题提供了正确的解决方案。 I will show his example snippet here: 我将在这里展示他的示例代码段:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell("reuseThis");
if(cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, "reuseThis");
}

cell.TextLabel.Text = Convert.ToChar(dash.oneToHundred[indexPath.Row]).ToString();

var button = UIButton.FromType(UIButtonType.RoundedRect);
button.Frame = new System.Drawing.RectangleF(40f, 0f, 280f, 40f);
button.SetTitle(dash.oneToHundred[indexPath.Row].ToString(),UIControlState.Normal);
button.AddTarget (this, new Selector ("MySelector"), UIControlEvent.TouchDown);
button.Tag = indexPath.Row;
cell.ContentView.AddSubview(button);

return cell;
}

[Export ("MySelector")]
void ButtonEventHere (UIButton button)
{

Console.WriteLine ("Hello! - Button clicked = " + button.Tag );
Console.WriteLine ( Convert.ToChar(dash.oneToHundred[button.Tag]).ToString() );
}

In short you call button.AddTarget() and you pass the corresponding values to the button. 简而言之,您调用button.AddTarget()并将相应的值传递给按钮。 one of these values is the selector which you defined as 'buttonEventHere' (can be any name you want it to me my sure that the [export("MySelector")] has the right name). 其中一个值是您定义为'buttonEventHere'的选择器(可以是您想要的任何名称,我确定[export(“MySelector”)]具有正确的名称)。 Make sure you pass the indexPath.Row to the button.Tag. 确保将indexPath.Row传递给button.Tag。 You will use that index to know which button was pressed and do your action accordingly. 您将使用该索引来了解按下了哪个按钮并相应地执行操作。

Hope this helps if you was stuck as i was. 希望如果你像我一样被困住,这会有所帮助。

You might want to use this technique to work out which UIButton was pressed rather than trying to use custom event args which is going to be problematic - http://jainmarket.blogspot.com/2009/05/custom-button-in-uitableviewcell.html 您可能希望使用此技术来确定按下哪个UIButton而不是尝试使用会出现问题的自定义事件参数 - http://jainmarket.blogspot.com/2009/05/custom-button-in-uitableviewcell html的

Essentially you just work out what section/row was selected in the UITableViewCell. 基本上你只是弄清楚在UITableViewCell中选择了哪个部分/行。 From there you should be able to work out what UIButton was selected. 从那里你应该能够找出UIButton的选择。

Hope this helps, 希望这可以帮助,

ChrisNTR ChrisNTR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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