Skip to content

Instantly share code, notes, and snippets.

@landonf
Created July 4, 2009 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save landonf/140395 to your computer and use it in GitHub Desktop.
Save landonf/140395 to your computer and use it in GitHub Desktop.
Playing with PLBlocks + UIActionSheet
// Copied from Apple's SeismicXML code sample
// When the user taps a row in the table, display the USGS web page that displays details of the earthquake they selected.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Earthquake *earthquake = (Earthquake *)[earthquakeList objectAtIndex:indexPath.row];
PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @"Display Map"];
[sheet addButtonWithTitle: @"Show USGS Site in Safari" block: ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: [earthquake USGSWebLink]]];
}];
[sheet addButtonWithTitle: @"Show Location in Maps" block: ^{
NSString *mapsQuery = [NSString stringWithFormat:@"z=6&t=h&ll=%f,%f", earthquake.latitude, earthquake.longitude];
mapsQuery = [mapsQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *mapsURLString = [kMapsBaseURL stringByAppendingString:mapsQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapsURLString]];
}];
[sheet setCancelButtonWithTitle: NSLocalizedString(@"Cancel", @"Cancel") block: ^{}];
[sheet showInView: self.view];
[sheet release];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//
// PLActionSheet.h
//
// Created by Landon Fuller on 7/3/09.
// Copyright 2009 Plausible Labs Cooperative, Inc.. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* A simple block-enabled API wrapper on top of UIActionSheet.
*/
@interface PLActionSheet : NSObject <UIActionSheetDelegate> {
@private
UIActionSheet *_sheet;
NSMutableArray *_blocks;
}
- (id) initWithTitle: (NSString *) title;
- (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block;
- (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block;
- (void) showInView: (UIView *) view;
@end
//
// PLActionSheet.m
//
// Created by Landon Fuller on 7/3/09.
// Copyright 2009 Plausible Labs Cooperative, Inc.. All rights reserved.
//
#import "PLActionSheet.h"
@implementation PLActionSheet
- (id) initWithTitle: (NSString *) title {
if ((self = [super init]) == nil)
return nil;
/* Initialize the sheet */
_sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil];
/* Initialize button -> block array */
_blocks = [[NSMutableArray alloc] init];
return self;
}
- (void) dealloc {
_sheet.delegate = nil;
[_sheet release];
[_blocks release];
[super dealloc];
}
- (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {
[self addButtonWithTitle: title block: block];
_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;
}
- (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {
[_blocks addObject: [[block copy] autorelease]];
[_sheet addButtonWithTitle: title];
}
- (void) showInView: (UIView *) view {
[_sheet showInView: view];
/* Ensure that the delegate (that's us) survives until the sheet is dismissed */
[self retain];
}
- (void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {
/* Run the button's block */
if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {
void (^b)() = [_blocks objectAtIndex: buttonIndex];
b();
}
/* Sheet to be dismissed, drop our self reference */
[self release];
}
@end
@newacct
Copy link

newacct commented Sep 20, 2011

addButtonWithTitle should return the index, like UIActionSheet does:

- (NSInteger) addButtonWithTitle: (NSString *) title block: (void (^)()) block {
    [_blocks addObject: [[block copy] autorelease]];
    return [_sheet addButtonWithTitle: title];
}

Then

[self addButtonWithTitle: title block: block];
_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;

should be written as

_sheet.cancelButtonIndex = [self addButtonWithTitle: title block: block];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment