Skip to content

Instantly share code, notes, and snippets.

@bnorton
Last active January 2, 2016 06:09
Show Gist options
  • Save bnorton/8261429 to your computer and use it in GitHub Desktop.
Save bnorton/8261429 to your computer and use it in GitHub Desktop.
UIAlertView subclass for block based handling of simple alert messages. Send in a title and message and specify a block to be called when the alert is dismissed.
//
// Alert.h
//
// Created by Brian Norton on 1/4/14.
// Copyright (c) 2014 Brian Norton. MIT.
//
#import <UIKit/UIKit.h>
@interface Alert : UIAlertView <UIAlertViewDelegate> {
void(^block)(BOOL);
id sself;
}
+(void)alertTitled:(NSString *)title message:(NSString *)message done:(void (^)(BOOL oked))done;
//
// Alert.m
//
// Created by Brian Norton on 1/4/14.
// Copyright (c) 2014 Brian Norton. MIT.
//
#import "Alert.h"
#define CANCEL_INDEX 0
@implementation Alert
+(void)alertTitled:(NSString *)t message:(NSString *)m done:(void (^)(BOOL))done {
Alert *alert = [[self alloc] initWithTitle:t message:m done:done];
[alert show];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message done:(void (^)(BOOL))done {
self = [super initWithTitle:title message:message delegate:nil
cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
if(!self) return nil;
sself = self;
block = done;
self.delegate = self;
return self;
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
sself = nil;
if(block)
block(buttonIndex != CANCEL_INDEX);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment