Timm
Timm

Reputation: 2662

Reuse MonoTouch.Dialog Element

For an application that dynamically changes which inputs are shown on the screen I create all possible Elements and then create a Section which contains the ones I need given the current data.

If the user interacts with a BooleanElement for example, the form would need to be updated and fields shown accordingly.

Apparently this pattern doesn't work, as shown by this test case:

        var dateEntry = new DateTimeElement("Timestamp", DateTime.Now);

        var section = new Section() { dateEntry };
        var root = new RootElement("Root") { section };
        var dvc = new DialogViewController(root);

        window.RootViewController = dvc;

        NSTimer.CreateScheduledTimer (TimeSpan.FromSeconds (2), delegate {
            var newSection = new Section() { dateEntry };
            dvc.Root.Clear();
            dvc.Root.Add(newSection);
        });

The above code throws a NullReferenceException in DateTimeElement.FormatDate once the timer is up.

I also tried recreating not only the Section but also the RootElement, but to no avail.

Is there any recommended pattern to reuse the Elements, or should I just create new inputs whenever the data changes?

Upvotes: 3

Views: 296

Answers (1)

Janub
Janub

Reputation: 1604

this should do the trick for you, and you can remove the NStimer

        this.Root.Remove(section);
        this.Root.Insert(0,UITableViewRowAnimation.Fade,newSection);

Upvotes: 3

Related Questions