I have two “nested” DialogViewControllers – a “View” and an “Edit”. Both render data from an internal data structure using StringElements. In the Edit DVC, I hook the StringElement’s Changed event to update my internal data structure. I also hook the Edit DVC’s ViewDissapearing (sic) event to re-render the View DVC, and also send the edits to a cloud service.
My issue is that the Changed event handler gets invoked after the ViewDissapearing event handler, so my internal data structure isn’t updated in time.
The code that sets up the DVC’s looks something like this:
var root = RenderViewItem(ThisItem);
var dvc = new DialogViewController(root, true);
// create an Edit button which pushes the edit view onto the nav stack
dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Edit, delegate {
var editRoot = RenderEditItem(ThisItem, true /* render the list field */);
editViewController = new DialogViewController(editRoot, true);
editViewController.ViewDissapearing += (sender, e) =>
{
// trigger a sync with the service when the view disappears
App.ViewModel.SyncWithService();
// reload the View page
dvc.BeginInvokeOnMainThread(() =>
{
var oldroot = root;
root = RenderViewItem(ThisItem);
dvc.Root = root;
dvc.ReloadData();
oldroot.Dispose();
});
};
controller.PushViewController(editViewController, true);
});
// push the "view item" view onto the nav stack
controller.PushViewController(dvc, true);
Inside RenderEditItem(), I add StringElements based on the internal data structure, and add the Changed EventHandler in the usual way:
stringElement.Changed += delegate { /* change a data structure */ };
Is there a way to get the Changed event handlers to fire before the ViewDissapearing event handler?
Good news! MonoTouch.Dialog version 5.2.11 made a behavior change to the way the EntryElement.Changed event is raised – it is now raised for every keystroke as opposed to only when a focus change happens. So my “race condition” no longer occurs and I am now able to eliminate the NSTimer hack.
A related benefit from this change is that you no longer have to call EntryElement.FetchValue() to ensure that EntryElement.Value is current.
http://docs.xamarin.com/ios/releases/MonoTouch_5/MonoTouch_5.2#11 for more info.