Nessinot
Nessinot

Reputation: 21

Custom title of MonoTouch.Dialog RootElement

I am trying to build a menu structure with Monotouch.Dialog. The structure consists of multiple nested RootElements.

When you create a RootElement you set the caption in the constructor. This caption is used for the text of the table cell as well as the title in the view that follows when you click on it.

I would like to set the title of the view to a different text than the name of the element.

Let my try to illustrate what I mean with a simplified example.

The structure:

- Item 1
  - Item 1.1
  - Item 1.2
- Item 2
  - Item 2.1

The code which creates this structure:

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow _window;
    UINavigationController _nav;
    DialogViewController _rootVC;
    RootElement _rootElement;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        _window = new UIWindow (UIScreen.MainScreen.Bounds);

        _rootElement = new RootElement("Main");

        _rootVC = new DialogViewController(_rootElement);
        _nav = new UINavigationController(_rootVC);

        Section section = new Section();
        _rootElement.Add (section);

        RootElement item1 = new RootElement("Item 1");
        RootElement item2 = new RootElement("Item 2");

        section.Add(item1);
        section.Add(item2);

        item1.Add   (
                        new Section()
                        {
                            new StringElement("Item 1.1"),
                            new StringElement("Item 1.2")
                        }
                    );

        item2.Add (new Section() {new StringElement("Item 2.1")});

        _window.RootViewController = _nav;
        _window.MakeKeyAndVisible ();

        return true;
    }
}

When Item 1 is clicked, a screen is displayed which has the title "Item 1". I would like to change the title "Item 1" to "Type 1 items". But the text of the element which was clicked should remain to be "Item 1".

What is the best way of handling this?

It would be nice if I could do something like this:

RootElement item1 = new RootElement("Item 1", "Type 1 items");

I have tried getting the DialogViewController (see this post) and setting the Title of it. But I failed in getting this to work properly.

Upvotes: 1

Views: 1209

Answers (1)

miguel.de.icaza
miguel.de.icaza

Reputation: 32694

You can create a subclass of RootElement, that overrides the return value from GetCell and changes the text to render when it is part of a table view:

 class MyRootElement : RootElement {
      string ShortName;

      public MyRootElement (string caption, string shortName)
          : base (caption)
      {
          ShortName = shortName;
      }

      public override UITableViewCell GetCell (UITableView tv)
      {
           var cell = base.GetCell (tv);
           cell.TextLabel.Text = ShortName;
           return cell;
      }
 }

Upvotes: 4

Related Questions