From 0b77472025a4b22103da9c8703c8fb406247d4d9 Mon Sep 17 00:00:00 2001 From: Brett Gibson Date: Tue, 10 May 2011 11:42:20 -0700 Subject: [PATCH 1/2] adding ability to specify action sheet title --- ActionSheetPicker.h | 6 +++++- ActionSheetPicker.m | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/ActionSheetPicker.h b/ActionSheetPicker.h index a1ee99671..f36afafb3 100644 --- a/ActionSheetPicker.h +++ b/ActionSheetPicker.h @@ -19,6 +19,7 @@ NSArray *_data; NSInteger _selectedIndex; + NSString *_title; UIDatePickerMode _datePickerMode; NSDate *_selectedDate; @@ -39,6 +40,7 @@ @property (nonatomic, retain) NSArray *data; @property (nonatomic, assign) NSInteger selectedIndex; +@property (nonatomic, copy) NSString *title; @property (nonatomic, assign) UIDatePickerMode datePickerMode; @property (nonatomic, retain) NSDate *selectedDate; @@ -56,12 +58,14 @@ //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; ++ (void)displayActionPickerWithView:(UIView *)aView title:(NSString *)title data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action; + //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; - (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; +- (id)initForDataWithContainingView:(UIView *)aView title:(NSString *)title data:(NSArray *)someData selectedIndex:(NSInteger)index target:(id)target action:(SEL)action; - (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action; //implementation diff --git a/ActionSheetPicker.m b/ActionSheetPicker.m index 2e6c562ca..a39400da2 100644 --- a/ActionSheetPicker.m +++ b/ActionSheetPicker.m @@ -15,6 +15,7 @@ @implementation ActionSheetPicker @synthesize data = _data; @synthesize selectedIndex = _selectedIndex; +@synthesize title = _title; @synthesize selectedDate = _selectedDate; @synthesize datePickerMode = _datePickerMode; @@ -32,12 +33,17 @@ @implementation ActionSheetPicker #pragma mark - #pragma mark NSObject -+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action { - ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action]; ++ (void)displayActionPickerWithView:(UIView *)aView title:(NSString *)title data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action { + ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView title:title data:data selectedIndex:selectedIndex target:target action:action]; actionSheetPicker.convenientObject = YES; [actionSheetPicker showActionPicker]; } ++ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action { + [self displayActionPickerWithView:aView title:nil data:data selectedIndex:selectedIndex target:target action:action]; +} + + + (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action { ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action]; actionSheetPicker.convenientObject = YES; @@ -54,10 +60,11 @@ - (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)actio return self; } -- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action { - if ([self initWithContainingView:aView target:target action:action] != nil) { +- (id)initForDataWithContainingView:(UIView *)aView title:(NSString *)title data:(NSArray *)data selectedIndex:(NSInteger)index target:(id)target action:(SEL)action{ + if ([self initWithContainingView:aView target:target action:action] != nil) { self.data = data; - self.selectedIndex = selectedIndex; + self.title = title; + self.selectedIndex = index; } return self; } @@ -75,7 +82,7 @@ - (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePicker - (void)showActionPicker { //spawn actionsheet - _actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; + _actionSheet = [[UIActionSheet alloc] initWithTitle:self.title delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; if (nil != self.data) @@ -180,7 +187,7 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f - (void)dealloc { - NSLog(@"ActionSheet Dealloc"); +// NSLog(@"ActionSheet Dealloc"); self.actionSheet = nil; self.pickerView.delegate = nil; From d95df2cd378d9d4a49c46e1e011cf203c9753bff Mon Sep 17 00:00:00 2001 From: Brett Gibson Date: Tue, 10 May 2011 11:42:54 -0700 Subject: [PATCH 2/2] removing duplicate files and referencing originals from sample project --- .gitignore | 16 ++ .../project.pbxproj | 21 +- Sample/Classes/ActionSheetPicker.h | 76 ------- Sample/Classes/ActionSheetPicker.m | 197 ------------------ 4 files changed, 31 insertions(+), 279 deletions(-) create mode 100644 .gitignore delete mode 100644 Sample/Classes/ActionSheetPicker.h delete mode 100644 Sample/Classes/ActionSheetPicker.m diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..64c810809 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Xcode +build/* +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +*.xcworkspace +!default.xcworkspace +xcuserdata +profile +*.moved-aside + diff --git a/Sample/ActionSheetPicker.xcodeproj/project.pbxproj b/Sample/ActionSheetPicker.xcodeproj/project.pbxproj index 47a62aa59..c0ee176d6 100644 --- a/Sample/ActionSheetPicker.xcodeproj/project.pbxproj +++ b/Sample/ActionSheetPicker.xcodeproj/project.pbxproj @@ -15,7 +15,7 @@ 2899E5220DE3E06400AC0155 /* ActionSheetPickerViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2899E5210DE3E06400AC0155 /* ActionSheetPickerViewController.xib */; }; 28AD733F0D9D9553002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD733E0D9D9553002E5188 /* MainWindow.xib */; }; 28D7ACF80DDB3853001CB0EB /* ActionSheetPickerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */; }; - BAE7C27212D58B8F001CEE19 /* ActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = BAE7C27112D58B8F001CEE19 /* ActionSheetPicker.m */; }; + 8B2B30A9130EF13700B90F79 /* ActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B2B30A8130EF13700B90F79 /* ActionSheetPicker.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -31,9 +31,9 @@ 28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActionSheetPickerViewController.m; sourceTree = ""; }; 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 32CA4F630368D1EE00C91783 /* ActionSheetPicker_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionSheetPicker_Prefix.pch; sourceTree = ""; }; + 8B2B30A7130EF13700B90F79 /* ActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetPicker.h; path = ../ActionSheetPicker.h; sourceTree = SOURCE_ROOT; }; + 8B2B30A8130EF13700B90F79 /* ActionSheetPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetPicker.m; path = ../ActionSheetPicker.m; sourceTree = SOURCE_ROOT; }; 8D1107310486CEB800E47090 /* ActionSheetPicker-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ActionSheetPicker-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = ""; }; - BAE7C27012D58B8F001CEE19 /* ActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionSheetPicker.h; sourceTree = ""; }; - BAE7C27112D58B8F001CEE19 /* ActionSheetPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActionSheetPicker.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -53,12 +53,12 @@ 080E96DDFE201D6D7F000001 /* Classes */ = { isa = PBXGroup; children = ( + 8B2B30A7130EF13700B90F79 /* ActionSheetPicker.h */, + 8B2B30A8130EF13700B90F79 /* ActionSheetPicker.m */, 1D3623240D0F684500981E51 /* ActionSheetPickerAppDelegate.h */, 1D3623250D0F684500981E51 /* ActionSheetPickerAppDelegate.m */, 28D7ACF60DDB3853001CB0EB /* ActionSheetPickerViewController.h */, 28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */, - BAE7C27012D58B8F001CEE19 /* ActionSheetPicker.h */, - BAE7C27112D58B8F001CEE19 /* ActionSheetPicker.m */, ); path = Classes; sourceTree = ""; @@ -139,7 +139,14 @@ isa = PBXProject; buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "ActionSheetPicker" */; compatibilityVersion = "Xcode 3.1"; + developmentRegion = English; hasScannedForEncodings = 1; + knownRegions = ( + English, + Japanese, + French, + German, + ); mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */; projectDirPath = ""; projectRoot = ""; @@ -169,7 +176,7 @@ 1D60589B0D05DD56006BFB54 /* main.m in Sources */, 1D3623260D0F684500981E51 /* ActionSheetPickerAppDelegate.m in Sources */, 28D7ACF80DDB3853001CB0EB /* ActionSheetPickerViewController.m in Sources */, - BAE7C27212D58B8F001CEE19 /* ActionSheetPicker.m in Sources */, + 8B2B30A9130EF13700B90F79 /* ActionSheetPicker.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -187,6 +194,7 @@ GCC_PREFIX_HEADER = ActionSheetPicker_Prefix.pch; INFOPLIST_FILE = "ActionSheetPicker-Info.plist"; PRODUCT_NAME = ActionSheetPicker; + SDKROOT = iphoneos; }; name = Debug; }; @@ -199,6 +207,7 @@ GCC_PREFIX_HEADER = ActionSheetPicker_Prefix.pch; INFOPLIST_FILE = "ActionSheetPicker-Info.plist"; PRODUCT_NAME = ActionSheetPicker; + SDKROOT = iphoneos; VALIDATE_PRODUCT = YES; }; name = Release; diff --git a/Sample/Classes/ActionSheetPicker.h b/Sample/Classes/ActionSheetPicker.h deleted file mode 100644 index a1ee99671..000000000 --- a/Sample/Classes/ActionSheetPicker.h +++ /dev/null @@ -1,76 +0,0 @@ -// -// 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 - - -@interface ActionSheetPicker : NSObject { - UIView *_view; - - NSArray *_data; - NSInteger _selectedIndex; - - UIDatePickerMode _datePickerMode; - NSDate *_selectedDate; - - 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, assign) UIDatePickerMode datePickerMode; -@property (nonatomic, retain) NSDate *selectedDate; - -@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; - -//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; - -- (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; -- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action; - -//implementation -- (void)showActionPicker; -- (void)showDataPicker; -- (void)showDatePicker; - -- (void)actionPickerDone; - -- (void)eventForDatePicker:(id)sender; - -@end diff --git a/Sample/Classes/ActionSheetPicker.m b/Sample/Classes/ActionSheetPicker.m deleted file mode 100644 index 2e6c562ca..000000000 --- a/Sample/Classes/ActionSheetPicker.m +++ /dev/null @@ -1,197 +0,0 @@ -// -// 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 selectedDate = _selectedDate; -@synthesize datePickerMode = _datePickerMode; - -@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 { - ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action]; - actionSheetPicker.convenientObject = YES; - [actionSheetPicker showActionPicker]; -} - -+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action { - ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action]; - 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 { - if ([self initWithContainingView:aView target:target action:action] != nil) { - self.data = data; - self.selectedIndex = selectedIndex; - } - return self; -} - -- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action { - if ([self initWithContainingView:aView target:target action:action] != nil) { - self.datePickerMode = datePickerMode; - self.selectedDate = selectedDate; - } - return self; -} - -#pragma mark - -#pragma mark Implementation - -- (void)showActionPicker { - //spawn actionsheet - _actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; - [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; - - if (nil != self.data) - //show data picker - [self showDataPicker]; - else - //show date picker - [self showDatePicker]; - - //spawn segmentedcontrol (faux done button) - UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]]; - - segmentedControl.momentary = YES; - segmentedControl.frame = CGRectMake(260, 7, 50, 30); - segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; - segmentedControl.tintColor = [UIColor blackColor]; - - [segmentedControl addTarget:self action:@selector(actionPickerDone) forControlEvents:UIControlEventValueChanged]; - - [self.actionSheet addSubview:segmentedControl]; - [segmentedControl release]; - - [self.actionSheet showInView:self.view]; - [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)]; -} - -- (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; - [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]; - [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.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 - -} - -#pragma mark - -#pragma mark Callbacks - -- (void)eventForDatePicker:(id)sender { - UIDatePicker *datePicker = (UIDatePicker *)sender; - - self.selectedDate = datePicker.date; -} - -#pragma mark - -#pragma mark UIPickerViewDelegate - -- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { - self.selectedIndex = row; -} - -#pragma mark - -#pragma mark UIPickerViewDataSource - -- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { - return 1; -} - -- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { - return self.data.count; -} - -- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { - return [self.data objectAtIndex:row]; -} - -#pragma mark - -#pragma mark Memory Management - - -- (void)dealloc { - NSLog(@"ActionSheet 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; - - [super dealloc]; -} - -@end