MonoTouch - reuse UIAlertVIew?
-
26-09-2019 - |
Question
Should instances of UIAlertView be reused when possible?
Or is wrapping them in a using-block the best practice?
- For one situation, I created a (non-modal) MessageBox class (just like WinForms or WPF). I mainly use it for convenience.
- Another situation is a custom UIAlertView that I've subclassed that has more advanced layout. I wire-up the Dismissed event to do some work on close.
Would either of these situation call for a different approach?
Solution
I dont think there's any problem doing this in a using block - you may want to abstract out the handler so that you dont have alert box delegates all over the place that do the same thing.
then again the LoadingHUDView is a good example of needing to code out the functionality for more advanced things...
but either way - if you're reusing the same object but really changing it each time - there;s not much to be gained IMO
OTHER TIPS
Here is how you could make your UIAlertView modal:
To do this, what you can do is to run the mainloop manually. I have not managed to stop the mainloop directly, so I instead run the mainloop for 0.5 seconds and wait until the user responds.
The following function shows how you could implement a modal query with the above approach:
int WaitForClick ()
{
int clicked = -1;
var x = new UIAlertView ("Title", "Message", null, "Cancel", "OK", "Perhaps");
x.Show ();
bool done = false;
x.Clicked += (sender, buttonArgs) => {
Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
clicked = buttonArgs.ButtonIndex;
};
while (clicked == -1){
NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
Console.WriteLine ("Waiting for another 0.5 seconds");
}
Console.WriteLine ("The user clicked {0}", clicked);
return clicked;
}