Is it possible to present a UIAlertView and not continue executing the rest of the code in that method until the user responds to the alert?
Thanks in advance.
If the code that needs to wait is all in the same method (or can be), blocks can be another option. I've created a UIAlertView subclass to handle this. Just keep in mind that execution will still continue past the [alert show] call; this just gives you a way to pass along your stack variables without making new in-between class variables. Also, if you aren't already familiar with Objective-C blocks, you should read the documentation on it; there are a few gotchas and some odd syntax you should be aware of.
Goes something like this:
typedef void (^MyAlertResult)(NSInteger clickedButtonIndex);
@interface MyBlockAlert : UIAlertView
{
MyAlertResult finishedBlock;
}
- (void) showWithCompletionBlock:(MyAlertResult)block;
...
- (void) showWithCompletionBlock:(MyAlertResult)block
{
self.delegate = self;
finishedBlock = [block copy];
[self show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
finishedBlock(buttonIndex);
[finishedBlock release];
finishedBlock = nil;
}
used like this:
__block NSArray* arrayOfDeletes; // set to something
MyBlockAlert* areYouSureAlert = [[MyBlockAlert alloc] initWithTitle:@"Really delete?" message:@"Message" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[arrayOfDeletes retain]; // Make sure retain count is +1
[areYouSureAlert showWithCompletionBlock:
^(NSInteger clickedButtonIndex)
{
if (clickedButtonIndex == 1)
{
// Clicked okay, perform delete
}
[arrayOfDeletes release];
}];
[areYouSureAlert release];
This was a quick implementation, so feel free to find any bugs; but you get the idea.