Using UIAlertView is similar to Windows' MessageBox ()?

I am new to iPhone, and I would like to be able to use UIAlertView in the same way as Windows MessageBox() or MessageDlg() in Delphi .

For example, I have a method that should ask the user to confirm something and continue based on their response.

eg. (Pseudo-code):

 -(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, say, in another way - is there a way to use UIAlertView to ask the user a question within a procedure as a way to control the logical flow of this procedure?

+4
source share
6 answers

I have no idea what MessageDlg() , but you can, of course, subclass UIAlertView and handle the response of the dialog based on which the button was clicked, for example:

Set the title of the subclass of UIAlertView :

 // // ARReachabilityAlertView.h // #import <UIKit/UIKit.h> @interface ARReachabilityAlertView : UIAlertView <UIAlertViewDelegate> { } @end 

Configure the implementation of the UIAlertView subclass:

 // // ARReachabilityAlertView.m // #import "ARReachabilityAlertView.h" @implementation ARReachabilityAlertView - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setTitle:@"Error"]; [self setMessage:@"This application won't run without a network connection. Do you want to quit?"]; [self addButtonWithTitle:@"Quit"]; [self addButtonWithTitle:@"Continue"]; [self setDelegate:self]; } return self; } - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) exit(0); // quit application if "Quit" is pressed; otherwise, do nothing } - (void) drawRect:(CGRect)rect { [super drawRect:rect]; } - (void) dealloc { [super dealloc]; } @end 

Note the delegate method alertView:clickedButtonAtIndex: This handles the conditional expressions that you use to decide how the application runs. You can send NSNotification from here or call the application’s deletion method, whatever you want.

In my example, this UIAlertView is created if there is no network connection, and the application closes if the user clicks the Exit button in the alert view. Otherwise, if the user clicks "Continue", the application continues to work as usual.

Note that to implement the subclass, you must call the drawRect: method. I'm not sure if this is a mistake or not, as I expect the drawRect: method to be called in the superclass; I filed a bug report with Apple, but I didn't hear anything. Comment on this if you want to see what effect will be - this is interesting.

+4
source

There is no reason to subclass UIAlertView at all. For this we need delegates. All you need is a class (for example, your view controller) that supports the UIAlertViewDelegate protocol, and set the UIAlertView delegation property class for it. Then you implement the alertView: clickedButtonAtIndex: method in this class and the alertViewCancel: method if you want to specifically handle the cancellation in different ways.

For more information, see the UIAlertView documentation and the UIAlertViewDelegate documentation .

+5
source

I believe that you want to use UIAlertView as a modal alert window (in the sense that you want your code to stop working until the user makes a choice). There is no easy way to do this, and he really recommended that you NOT code your iPhone in this way. I think Alex's explanation is a good solution.

+2
source

To do this, you can simply start mainloop manually. I was not able to stop mainloop directly, so instead I started mainloop for 0.5 seconds and wait for the user to respond.

I came across this question while exploring a problem for a C # / MonoTouch user on an iPhone. The example below is written for MonoTouch / C #, but should be trivial to translate to Objective-C

The following function shows how you could implement a modal query using 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; } 
+2
source

Just tell me: if you want the class to be a delegate for more than one UIAlertView, just use the tag property to find out who is who:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Clear all" message:@"Are you sure you want to erase everything?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; alert.tag = ALERT_DELETE_TAG; [alert autorelease]; [alert show]; 

Delegate methods are called using UIAlertView as the first argument, and you can check who the sender is.

+2
source

Thanks to @ miguel.de.icaza I found my solution as shown below ( part of the code ):

 @interface MyClass: NSObject <UIAlertViewDelegate> { int confirmed; } - (BOOL) removeObjectAtIndex:(NSUInteger)index; @end @implementation - (BOOL) removeObjectAtIndex:(NSUInteger)index { // delete confirmation alert confirmed = -1; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete confirmation" message:@"Are you sure to delete object" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles: @"No", nil]; alert.tag = 2; [alert show]; [alert release]; // wait for confirm (0 or 1) while (confirmed == -1) { // this is what you need!!! [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]]; } if (confirmed) { [myObjects removeObjectAtIndex:index]; return YES; } else return NO; } - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch (alertView.tag) { case 1: // ... break; case 2: if (buttonIndex == 0) // delete confirmed confirmed = 1; else // dismiss confirmed = 0; break; default: break; } } @end 
+2
source

All Articles