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 111 lines (86 sloc) 3.096 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 106 107 108 109 110
//
// PLActionSheet.m
//
// Created by Landon Fuller on 7/3/09.
// Copyright 2009 Plausible Labs Cooperative, Inc.. All rights reserved.
//

#import "PSActionSheet.h"

@interface PSActionSheet ()
@property (nonatomic, retain, readwrite) UIActionSheet *sheet;
@end

@implementation PSActionSheet
@synthesize sheet = sheet_;

+ (id)sheetWithTitle:(NSString *)title {
  return [[[PSActionSheet alloc] initWithTitle:title] autorelease];
}


- (id)initWithTitle:(NSString *)title {
  if ((self = [super init])) {
    // Create the action sheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    self.sheet = actionSheet;
    [actionSheet release];

    // Create the blocks storage for handling all button actions
    blocks_ = [[NSMutableArray alloc] init];
  }

  return self;
}

- (void) dealloc {
  self.sheet.delegate = nil;
  self.sheet = nil;
  [blocks_ release];
  [super dealloc];
}

- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block {
  assert([title length] > 0 && "sheet destructive button title must not be empty");

  [self addButtonWithTitle:title block:block];
  sheet_.destructiveButtonIndex = (sheet_.numberOfButtons - 1);
}

- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block {
  assert([title length] > 0 && "sheet cancel button title must not be empty");

  [self addButtonWithTitle:title block:block];
  sheet_.cancelButtonIndex = (sheet_.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]];
  }

  [sheet_ addButtonWithTitle:title];
}

- (void)showInView:(UIView *)view {
  [sheet_ showInView:view];

  // Ensure that the delegate (that's us) survives until the sheet is dismissed.
  // Release occurs in -actionSheet:clickedButtonAtIndex:
  [self retain];
}

- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated {
  [sheet_ showFromBarButtonItem:item animated:animated];
  [self retain];
}

- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated {
  [sheet_ showFromRect:rect inView:view animated:animated];
  [self retain];
}

- (void)actionSheet:(UIActionSheet *)actionSheet 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)();
    }
  }

  // Sheet to be dismissed, drop our self reference.
  // Retain occurs in -showInView:
  [self release];
}

- (NSUInteger)buttonCount {
  return [blocks_ count];
}

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