Created
July 4, 2009 01:45
-
-
Save landonf/140395 to your computer and use it in GitHub Desktop.
Playing with PLBlocks + UIActionSheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
addButtonWithTitle should return the index, like UIActionSheet does:
Then
should be written as