Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 106 lines (81 sloc) 3.145 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//
// PSAlertView.m
//
// Created by Peter Steinberger on 17.03.10.
// Loosely based on Landon Fullers "Using Blocks", Plausible Labs Cooperative.
// http://landonf.bikemonkey.org/code/iphone/Using_Blocks_1.20090704.html
//

#import "PSAlertView.h"

@implementation PSAlertView

@synthesize alertView = view_;

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Static

+ (PSAlertView *)alertWithTitle:(NSString *)title; {
    return [[[PSAlertView alloc] initWithTitle:title message:nil] autorelease];
}

+ (PSAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message; {
    return [[[PSAlertView alloc] initWithTitle:title message:message] autorelease];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NSObject

- (id)initWithTitle:(NSString *)title message:(NSString *)message {
    if ((self = [super init])) {
        view_ = [[UIAlertView alloc] initWithTitle:title
                                           message:message
                                          delegate:self
                                 cancelButtonTitle:nil
                                 otherButtonTitles:nil];
        blocks_ = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)dealloc {
    view_.delegate = nil;
    MCRelease(view_);
    MCRelease(blocks_);
    [super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public

- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block {
    assert([title length] > 0 && "cannot set empty button title");
    
    [self addButtonWithTitle:title block:block];
    view_.cancelButtonIndex = (view_.numberOfButtons - 1);
}

- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block {
    assert([title length] > 0 && "cannot add button with empty title");
    
    if (block) {
        [blocks_ addObject:[[block copy] autorelease]];
    }
    else {
        [blocks_ addObject:[NSNull null]];
    }
    
    [view_ addButtonWithTitle:title];
}

- (void)show {
    [view_ show];
    
    /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
    [self retain];
}


- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
    [view_ dismissWithClickedButtonIndex:buttonIndex animated:animated];
    [self alertView:view_ clickedButtonAtIndex:buttonIndex];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    /* Run the button's block */
    if (buttonIndex >= 0 && buttonIndex < [blocks_ count]) {
        id obj = [blocks_ objectAtIndex: buttonIndex];
        if (![obj isEqual:[NSNull null]]) {
            ((void (^)())obj)();
        }
    }
    
    /* AlertView to be dismissed, drop our self reference */
    [self release];
}

@end
Something went wrong with that request. Please try again.