Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing popup when RadioGroup Item Selected

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?

like image 326
russellj Avatar asked Dec 02 '11 23:12

russellj


1 Answers

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;
}
like image 81
Jeff Lindborg Avatar answered Oct 16 '22 03:10

Jeff Lindborg