Ian Vink
Ian Vink

Reputation: 68750

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?

Upvotes: 7

Views: 1471

Answers (1)

Waescher
Waescher

Reputation: 5737

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.

Upvotes: 13

Related Questions