I’m new to the iPhone and I would like to be able to use UIAlertView in a manner similar to the Windows MessageBox() or the MessageDlg() in Delphi.
For example, I have a method that needs to ask the user for confirmation on something, and proceed based on their response.
E.g. (pseudocode):
-(void)doSomething
{
[doStep1];
[doStep2];
var myValue = [getDefaultValue];
if (myValue = nil)
{
if [promptUser(@"No value in setting. Use the default value?")] //UIAlertView here?
{
myValue = @"defaultValue";
}
else
return; // bug out of the routine 'cause we have no value.
}
[doStep3 withValue:myValue];
}
Or, put put it another way- is there a way of using UIAlertView to ask the user a question within a routine as a way of controlling the logic flow of that routine?
I have no idea what
MessageDlg()is, but you can certainly subclassUIAlertViewand handle the dialog response, based on which button is pressed, e.g.:Set up the
UIAlertViewsubclass header:Set up the
UIAlertViewsubclass implementation:Note the
alertView:clickedButtonAtIndex:delegate method. This handles the conditionals you use to decide how the application proceeds. You can send anNSNotificationfrom here, or call a method in the application delegate, whatever you want.In my example, this
UIAlertViewis instantiated if there is no network connection, and the application is closed if the user clicks “Quit” in the alert view. Otherwise, if the user clicks “Continue” the application keeps running as usual.Note that the implementation of the subclass requires the
drawRect:method be called. I’m not sure if this is a bug or not, since I’d expect thedrawRect:method to be called in the super-class; I filed a bug report with Apple on this one, but I haven’t heard anything. Comment it out if you want to see what the effect will be — it’s kind of interesting.