A modal UIAlertView can be implemented completely differently than we did in our code snippet of the week. Erica Sadun’s iPhone cookbook provides an elegant solution for this problem. In a nutshell, the idea is to utilize the runloop as shown in the code snippet below.
#import "ModalAlert.h"
@interface ModalAlertDelegate : NSObject uialertviewdelegate
{
CFRunLoopRef currentLoop;
NSUInteger index;
}
@property (readonly) NSUInteger index;
@end
@implementation ModalAlertDelegate
@synthesize index;
-(id) initWithRunLoop: (CFRunLoopRef)runLoop
{
if (self = [super init]) currentLoop = runLoop;
return self;
}
-(void) alertView: (UIAlertView*)aView clickedButtonAtIndex: (NSInteger)anIndex
{
index = anIndex;
CFRunLoopStop(currentLoop);
}
@end
@implementation ModalAlert
+(NSUInteger) queryWith: (NSString *)question button1: (NSString *)button1 button2: (NSString *)button2
{
CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
ModalAlertDelegate *madelegate = [[ModalAlertDelegate alloc] initWithRunLoop:currentLoop];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:question message:nil delegate:madelegate cancelButtonTitle:button1 otherButtonTitles:button2, nil];
[alertView show];
CFRunLoopRun();
NSUInteger answer = madelegate.index;
[alertView release];
[madelegate release];
return answer;
}
+ (BOOL) ask: (NSString *) question
{
return ([ModalAlert queryWith:question button1: @"Yes" button2: @"No"] == 0);
}
+ (BOOL) confirm: (NSString *) statement
{
return [ModalAlert queryWith:statement button1: @"Cancel" button2: @"OK"];
}
@end
your coding ikangai team





