Terribly sorry, I don't know how to use github, pretty new here. I really like your library, and made a few improvement to support multiple components, size, and also support minimum date.
//
// ActionSheetPicker.h
// Spent
//
// Created by Tim Cinel on 3/01/11.
// Copyright 2011 Thunderous Playground. All rights reserved.
//
//
// Easily present an ActionSheet with a PickerView to select from a number of immutible options,
// based on the drop-down replacement in mobilesafari.
//
// Some code derived from marcio's post on Stack Overflow [ http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how ]
import <Foundation/Foundation.h>
@interface ActionSheetPicker : NSObject <UIPickerViewDelegate, UIPickerViewDataSource> {
UIView *_view;
NSArray *_data;
NSInteger _selectedIndex;
NSArray *_multipleComponentsData;
NSMutableArray *_multipleComponentsSelectedIndex;
NSArray *_componentWidths;
NSString *_title;
UIDatePickerMode _datePickerMode;
NSDate *_selectedDate;
NSDate *_minimumDate;
id _target;
SEL _action;
UIActionSheet *_actionSheet;
UIPickerView *_pickerView;
UIDatePicker *_datePickerView;
NSInteger _pickerPosition;
BOOL _convenientObject;
}
@Property (nonatomic, retain) UIView *view;
@Property (nonatomic, retain) NSArray *data;
@Property (nonatomic, assign) NSInteger selectedIndex;
@Property (nonatomic, retain) NSArray *multipleComponentsData;
@Property (nonatomic, retain) NSMutableArray *multipleComponentsSeletedIndex;
@Property (nonatomic, retain) NSArray *componentWidths;
@Property (nonatomic, copy) NSString *title;
@Property (nonatomic, assign) UIDatePickerMode datePickerMode;
@Property (nonatomic, retain) NSDate *selectedDate;
@Property (nonatomic, retain) NSDate *minimumDate;
@Property (nonatomic, retain) id target;
@Property (nonatomic, assign) SEL action;
@Property (nonatomic, retain) UIActionSheet *actionSheet;
@Property (nonatomic, retain) UIPickerView *pickerView;
@Property (nonatomic, retain) UIDatePicker *datePickerView;
@Property (nonatomic, assign) NSInteger pickerPosition;
@Property (nonatomic, assign) BOOL convenientObject;
//no memory management required for convenience methods
//display actionsheet picker inside View, loaded with strings from data, with item selectedIndex selected. On dismissal, [target action:(NSNumber *)selectedIndex] is called
//+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title;
//display actionsheet datepicker in datePickerMode inside View with selectedDate selected. On dismissal, [target action:(NSDate *)selectedDate] is called
//+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;
- (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action;
- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)someData selectedIndex:(NSInteger)index target:(id)target action:(SEL)action title:(NSString *)title;
- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;
- (id)initForDateWithContainingView:(UIView )aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate)minimumDate target:(id)target action:(SEL)action title:(NSString *)title;
- (id)initForDataWithContainingView:(UIView )aView
multipleComponentsData:(NSArray *)someData
multipleComponentsSelectedIndex:(NSMutableArray)indexArray
componentWidths:(NSArray*)componentWidths
target:(id)target
action:(SEL)action
title:(NSString *)title;
//implementation
- (void)showActionPicker;
- (void)showDataPicker;
- (void)showDatePicker;
- (void)actionPickerDone;
- (void)actionPickerCancel;
- (void)eventForDatePicker:(id)sender;
- (BOOL)isViewPortrait;
@EnD
//
// ActionSheetPicker.m
// Spent
//
// Created by Tim Cinel on 3/01/11.
// Copyright 2011 Thunderous Playground. All rights reserved.
//
import "ActionSheetPicker.h"
@implementation ActionSheetPicker
@synthesize view = _view;
@synthesize data = _data;
@synthesize selectedIndex = _selectedIndex;
@synthesize multipleComponentsData = _multipleComponentsData;
@synthesize multipleComponentsSeletedIndex = _multipleComponentsSelectedIndex;
@synthesize componentWidths = _componentWidths;
@synthesize title = _title;
@synthesize selectedDate = _selectedDate;
@synthesize datePickerMode = _datePickerMode;
@synthesize minimumDate = _minimumDate;
@synthesize target = _target;
@synthesize action = _action;
@synthesize actionSheet = _actionSheet;
@synthesize pickerView = _pickerView;
@synthesize datePickerView = _datePickerView;
@synthesize pickerPosition = _pickerPosition;
@synthesize convenientObject = _convenientObject;
pragma mark -
pragma mark NSObject
//+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
// ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action title:title];
// actionSheetPicker.convenientObject = YES;
// [actionSheetPicker showActionPicker];
//}
//
//+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title{
// ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title];
// actionSheetPicker.convenientObject = YES;
// [actionSheetPicker showActionPicker];
//}
- (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action {
if ((self = [super init]) != nil) {
self.view = aView;
self.target = target;
self.action = action;
}
return self;
}
- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title{
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.data = data;
self.selectedIndex = selectedIndex;
self.title = title;
}
return self;
}
- (id)initForDataWithContainingView:(UIView )aView
multipleComponentsData:(NSArray *)someData
multipleComponentsSelectedIndex:(NSMutableArray)indexArray
componentWidths:(NSArray*)componentWidths
target:(id)target
action:(SEL)action
title:(NSString *)title {
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.multipleComponentsData = someData;
self.multipleComponentsSeletedIndex = indexArray;
self.title = title;
self.componentWidths = componentWidths;
}
return self;
}
- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title {
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.datePickerMode = datePickerMode;
self.selectedDate = selectedDate;
self.title = title;
}
return self;
}
- (id)initForDateWithContainingView:(UIView )aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate)minimumDate target:(id)target action:(SEL)action title:(NSString *)title {
if ([self initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title] != nil) {
self.minimumDate = minimumDate;
};
return self;
}
pragma mark -
pragma mark Implementation
-
(void)showActionPicker {
//spawn actionsheet
_actionSheet = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?nil:@"\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
if (nil != self.data || nil != self.multipleComponentsData)
//show data picker
[self showDataPicker];
else
//show date picker
[self showDatePicker];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 480, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
[barItems addObject:flexSpace];
//Add tool bar title label
if (nil != self.title){
UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = self.title;
UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
[toolBarItemlabel release];
[barItems addObject:buttonLabel];
[buttonLabel release];
[barItems addObject:flexSpace];
}
//add "Done" button
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone)];
[barItems addObject:barButton];
[barButton release];
[pickerDateToolbar setItems:barItems animated:YES];
[barItems release];
[self.actionSheet addSubview:pickerDateToolbar];
[pickerDateToolbar release];
[self.actionSheet showInView:self.view];
if ( [self isViewPortrait] )
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
else
[self.actionSheet setBounds:CGRectMake(0, 0, 480, 325)];
}
-
(void)showDataPicker {
//spawn pickerview
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
_pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
if (self.multipleComponentsSeletedIndex != nil) {
for (int component=0; component<[self.multipleComponentsSeletedIndex count]; component++) {
NSNumber *number = [self.multipleComponentsSeletedIndex objectAtIndex:component];
[self.pickerView selectRow:[number intValue] inComponent:component animated:NO];
}
}
else {
[self.pickerView selectRow:self.selectedIndex inComponent:0 animated:NO];
}
[self.actionSheet addSubview:self.pickerView];
}
-
(void)showDatePicker {
//spawn datepickerview
CGRect datePickerFrame = CGRectMake(0, 40, 0, 0);
_datePickerView = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
self.datePickerView.datePickerMode = self.datePickerMode;
[self.datePickerView setDate:self.selectedDate];
// To support minimum date
if (_minimumDate != nil) {
[self.datePickerView setMinimumDate:self.minimumDate];
}
[self.datePickerView addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:self.datePickerView];
}
-
(void)actionPickerDone {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
if (nil != self.multipleComponentsData) {
//send multiple data picker message
[self.target performSelector:self.action withObject:self.multipleComponentsSeletedIndex];
}
else if (nil != self.data) {
//send data picker message
[self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex]];
} else {
//send date picker message
[self.target performSelector:self.action withObject:self.selectedDate];
}
if (self.convenientObject)
[self release]; //release convenient object
}
- (void)actionPickerCancel {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
- (BOOL) isViewPortrait {
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
pragma mark -
pragma mark Callbacks
pragma mark -
pragma mark UIPickerViewDelegate
-
(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (self.multipleComponentsData != nil) {
NSNumber *number = [NSNumber numberWithInt:row];
[self.multipleComponentsSeletedIndex replaceObjectAtIndex:component withObject:number];
}
else {
self.selectedIndex = row;
}
}
pragma mark -
pragma mark UIPickerViewDataSource
-
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// For multiple components: if data is an array contains other arrays,
// each array will represent data in one component
if (self.multipleComponentsData != nil) {
return [self.multipleComponentsData count];
}
return 1;
}
-
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// For multiple components: if data is an array contains other arrays,
// each array will represent data in one component
if (self.multipleComponentsData != nil) {
return [[self.multipleComponentsData objectAtIndex:component] count];
}
// For one section
return self.data.count;
}
-
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// For multiple components: if data is an array contains other arrays,
// each array will represent data in one component
if (self.multipleComponentsData != nil) {
return [[self.multipleComponentsData objectAtIndex:component] objectAtIndex:row];
}
// For 1 section
return [self.data objectAtIndex:row];
}
// Custom, depend on specific situation
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (self.componentWidths != nil) {
return [[self.componentWidths objectAtIndex:component] intValue];
}
// If componentWidths is nil
return 300;
}
pragma mark -
pragma mark Memory Management
-
(void)dealloc {
self.actionSheet = nil;
self.pickerView.delegate = nil;
self.pickerView.dataSource = nil;
self.pickerView = nil;
[self.datePickerView removeTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];
self.datePickerView = nil;
self.selectedDate = nil;
[_multipleComponentsData release];
[_multipleComponentsSelectedIndex release];
[_componentWidths release];
[super dealloc];
}
@EnD