2

MonotouchDialog makes it very easy to create UITableView Dialogs, but sometimes questions like that one popup:

MonoTouch Dialog. Buttons with the Elements API

Now, I have a similar problem but quite different:

List<User> users = GetUsers();

var root = 
   new RootElement ("LoginScreen"){
     new Section ("Enter your credentials") {
       foreach(var user in users)
         new StyledStringElement (user.Name, ()=> {
            // tap on an element (but which one exactly?)
         }
     ),
   }

navigation.PushViewController (new MainController (root), true);

Now, the second parameter of StyledStringElement's constructor has the type of NSAction delegate, and doesn't take any arguments, now I dunno how to determine exactly which element been tapped.

How to get that?

Community
  • 1
  • 1
iLemming
  • 34,477
  • 60
  • 195
  • 309

2 Answers2

3

If it was Tapped then it has been selected. So you should be able to inherit from StyleStringElement and override its Selected method to accomplish the same goal.

e.g.

class UserElement : StyleStingElement {
    public UserElement (User user) { ... }

    public override Selected (...)
    {
        // do your processing on 'user'
        base.Selected (dvc, tableView, indexPath);
    }
}

For Touch.Unit I created a new *Element for every item I had, TestSuiteElement, TestCaseElement, TestResultElement... to be able to customize each of them and adapt (a bit) their behaviour but I did not use this Selected to replace Tapped. You might want to check but it would not fit with your code pattern to create elements.

poupou
  • 43,413
  • 6
  • 77
  • 174
2

"...a Flower by any other name?"

If you look closely NSAction's are just delegates. I prefer to pass Action / Func into those params the reference for which is contained within the...container controller.

So lets so you have a UINavigationController that pushes a DialogViewController. When your element is selected you provide the unique user that you've passed to the Element and go from there :-)

public class MyNavController : UINavigationController
{
    Action<User> UserClickedAction;

    public MyNavController()
    {
        UserClickedAction = HandleUserClicked;
    }

    public void HandleUserClicked(User user)
    {
        ...
    }
}
Anuj
  • 3,134
  • 13
  • 17