Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monotouch.Dialog - Which Element was Tapped

I have a list of Customers which I use to create Elements like this:

Foreach(Customer c in Customers)
{
    //Make the StyledStringElement
    //Set the Tapped to action a touch
    element.Tapped += () => {  Push (new SomeController (c.ClientId)); };
}

The problem is that when the element is tapped it sends the last customer to SomeController().

How can I set the Tapped Delegate with information that will id the customer?

like image 349
Ian Vink Avatar asked Apr 18 '11 08:04

Ian Vink


1 Answers

You need to keep the customer as local variable in the loop:

foreach(Customer c in Customers)
{    
    //Make the StyledStringElement
    //Set the Tapped to action a touch
    var currentCustomer = c;
    element.Tapped += () => {  Push (new SomeController (currentCustomer.ClientId)); };
}

But this is not a limitation with MonoTouch.Dialog. Here's an article about the general problem.

like image 112
Waescher Avatar answered Nov 06 '22 01:11

Waescher