Dismissing popup when RadioGroup Item Selected
Asked Answered
A

1

5

Using the awesome MonoTouch.dialog in some of my projects now and have a question. I have a RadioGroup that I use to allow the user to select his home state, States is a string array.

    public static RootElement CreateStates ()
    {
        return new RootElement ("State", new RadioGroup (0)) 
        {
            new Section ("Choose State")
            {
                from x in States
                   select (Element) new RadioElement (x) 
            }
        };
    }

This works fine, and when I select state the popup appears and I pick my state, but then I have to hit the back button in the nav bar to get back to my main screen. Is there a way to have that popup dismiss when I select a choice? Having to hit the back button is annoying. Or am I just using the wrong solution to this altogether?

My first thought was to subclass RadioElement and catch the selected event, but then I still wasn't sure how to dismiss the automatic selection popup?

Archetype answered 2/12, 2011 at 23:24 Comment(0)
H
13

I ran into the same problem this morning and I also wanted to trigger on an event for the change so I could add a "cancel" button on the dialog when data had been edited. Both tasks require you to subclass RadioElement and override the Selected method - note the extra step to ensure the dialog does NOT close if the user clicks the already selected item - it will fire if you click on anything even if its already selected so I wanted to protect against that - mine looks like this.

public class MyRadioElement : RadioElement {
    // Pass the caption through to the base constructor.
    public MyRadioElement (string pCaption) : base(pCaption) {
    }

    // Fire an event when the selection changes.
    // I use this to flip a "dirty flag" further up stream.
    public override void Selected (
        DialogViewController pDialogViewController, 
        UITableView pTableView, NSIndexPath pIndexPath) {
        // Checking to see if the current cell is already "checked"
        // prevent the event from firing if the item is already selected.  
        if (GetActiveCell().Accessory.ToString().Equals(
            "Checkmark",StringComparison.InvariantCultureIgnoreCase)) {
            return;
        }

        base.Selected (pDialogViewController, pTableView, pIndexPath);

        // Is there an event mapped to our OnSelected event handler?
        var selected = OnSelected;

        // If yes, fire it.
        if (selected != null) {
            selected (this, EventArgs.Empty);
        }

        // Close the dialog.
        pDialogViewController.DeactivateController(true);
    }

    static public event EventHandler<EventArgs> OnSelected;
}
Hillel answered 22/12, 2011 at 17:11 Comment(2)
You are the man! Thanks so much. I was close, it was how to close out the dialog that I was missing. This is exactly what I needed.Archetype
Great. I think I can use this as well :P. russellj can you mark this as the accepted answer?Felony

© 2022 - 2024 — McMap. All rights reserved.