Skip to content
Browse files

More refactoring. Uses an abstract picker class. Added blocks to the …

…strings picker for illustration.

Lots of refactoring … To help the pickers "do one thing", I've built an abstract class that manages just the bare necessities.  Other pickers derive from it (strings, dates, distances). This will certainly break old implementations, however, the necessary changes are easy.  Also, added blocks to the strings picker … others could benefit too, but this strings picker class was my primary focus.
  • Loading branch information...
1 parent 97cc59e commit 222f908b8c09d2fa256dd3fcc0eee3ca119ad370 @grgcombs grgcombs committed
View
50 ActionSheetPicker.h 100755 → 100644
@@ -25,51 +25,9 @@
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
-#import <Foundation/Foundation.h>
+#import "AbstractActionSheetPicker.h"
+#import "ActionSheetStringPicker.h"
+#import "ActionSheetDatePicker.h"
+#import "ActionSheetDistancePicker.h"
-@protocol ActionSheetPickerDelegate<NSObject>
-- (void)actionPickerCancelled;
-@optional
-- (void)actionPickerDoneWithValue:(id)value;
-@end
-
-@interface ActionSheetPicker : NSObject <UIPickerViewDelegate, UIPickerViewDataSource>
-
-@property (nonatomic, assign) id<ActionSheetPickerDelegate> delegate;
-@property (nonatomic, retain) UIView *pickerView;
-@property (nonatomic, readonly) CGSize viewSize;
-@property (nonatomic, retain) NSMutableArray *customButtons; // ((NSString *title, id value),(NSString *title, id value), ... )
-@property (nonatomic, assign) BOOL hideCancel;
-
-/* Create and display an action sheet picker. The returned picker is autoreleased.
- "origin" must not be empty. It can be either an originating container view or a UIBarButtonItem to use with a popover arrow.
- "target" must not be empty. It should respond to "onSuccess" actions.
- "rows" is an array of strings to use for the picker's available selection choices.
- "initialSelection" is used to establish the initially selected row;
- */
-+ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target action:(SEL)action origin:(id)origin;
-
- // Create an action sheet picker, but don't display until a subsequent call to "showActionPicker". Receiver must release the picker when ready. */
-- (id)initWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target action:(SEL)action origin:(id)origin;
-
-
- // Adds custom buttons to the left of the UIToolbar that select specified values
-- (void)addCustomButtonWithTitle:(NSString *)title value:(id)value;
-
- // Present the ActionSheetPicker
-- (void)showActionSheetPicker;
-
- // For subclasses. This is used to send a message to the target upon a successful selection and dismissal of the picker (i.e. not canceled).
-- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)origin;
-
- // For subclasses. This returns a configured picker view. Subclasses should autorelease.
-- (UIPickerView *)configuredPickerView;
-
-- (void)actionPickerDone:(id)sender;
-- (void)actionPickerCancel:(id)sender;
-
- //For subclasses. This responds to a custom button being pressed.
-- (void)customButtonPressed:(id)sender;
-
-@end
View
59 Pickers/AbstractActionSheetPicker.h
@@ -0,0 +1,59 @@
+//
+//Copyright (c) 2011, Tim Cinel
+//All rights reserved.
+//
+//Redistribution and use in source and binary forms, with or without
+//modification, are permitted provided that the following conditions are met:
+//* Redistributions of source code must retain the above copyright
+//notice, this list of conditions and the following disclaimer.
+//* Redistributions in binary form must reproduce the above copyright
+//notice, this list of conditions and the following disclaimer in the
+//documentation and/or other materials provided with the distribution.
+//* Neither the name of the <organization> nor the
+//names of its contributors may be used to endorse or promote products
+//derived from this software without specific prior written permission.
+//
+//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface AbstractActionSheetPicker : NSObject
+@property (nonatomic, copy) NSString *title;
+@property (nonatomic, retain) UIView *pickerView;
+@property (nonatomic, readonly) CGSize viewSize;
+@property (nonatomic, retain) NSMutableArray *customButtons;
+@property (nonatomic, assign) BOOL hideCancel;
+
+ // For subclasses.
+- (id)initWithTarget:(id)target successAction:(SEL)successAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin;
+
+ // Present the ActionSheetPicker
+- (void)showActionSheetPicker;
+
+ // For subclasses. This is used to send a message to the target upon a successful selection and dismissal of the picker (i.e. not canceled).
+- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)sucessAction origin:(id)origin;
+
+ // For subclasses. This is an optional message upon cancelation of the picker.
+- (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin;
+
+ // For subclasses. This returns a configured picker view. Subclasses should autorelease.
+- (UIPickerView *)configuredPickerView;
+
+ // Adds custom buttons to the left of the UIToolbar that select specified values
+- (void)addCustomButtonWithTitle:(NSString *)title value:(id)value;
+
+ //For subclasses. This responds to a custom button being pressed.
+- (void)customButtonPressed:(id)sender;
+
+@end
View
320 ActionSheetPicker.m → Pickers/AbstractActionSheetPicker.m
@@ -25,23 +25,20 @@
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
-#import "ActionSheetPicker.h"
+#import "AbstractActionSheetPicker.h"
+#import <objc/message.h>
-@interface ActionSheetPicker()
+@interface AbstractActionSheetPicker()
@property (nonatomic, retain) UIBarButtonItem *barButtonItem;
-@property (nonatomic, retain) NSArray *data;
@property (nonatomic, retain) UIView *containerView;
-@property (nonatomic, assign) NSInteger selectedIndex;
-@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) id target;
-@property (nonatomic, assign) SEL action;
+@property (nonatomic, assign) SEL successAction;
+@property (nonatomic, assign) SEL cancelAction;
@property (nonatomic, retain) UIActionSheet *actionSheet;
@property (nonatomic, retain) UIPopoverController *popOverController;
@property (nonatomic, retain) NSObject *selfReference;
-- (ActionSheetPicker *)initWithTarget:(id)target action:(SEL)action origin:(id)origin;
-
- (void)presentPickerForView:(UIView *)aView;
- (void)configureAndPresentPopoverForView:(UIView *)aView;
- (void)configureAndPresentActionSheetForView:(UIView *)aView;
@@ -54,44 +51,34 @@ - (id)storedOrigin;
- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle;
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)aTitle;
- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction;
-
+- (IBAction)actionPickerDone:(id)sender;
+- (IBAction)actionPickerCancel:(id)sender;
@end
-@implementation ActionSheetPicker
-
+@implementation AbstractActionSheetPicker
+@synthesize title = _title;
@synthesize containerView = _containerView;
@synthesize barButtonItem = _barButtonItem;
-@synthesize data = _data;
-@synthesize selectedIndex = _selectedIndex;
-@synthesize title = _title;
@synthesize target = _target;
-@synthesize action = _action;
+@synthesize successAction = _successAction;
+@synthesize cancelAction = _cancelAction;
@synthesize actionSheet = _actionSheet;
@synthesize popOverController = _popOverController;
@synthesize selfReference = _selfReference;
-
-@synthesize delegate = _delegate;
@synthesize pickerView = _pickerView;
@dynamic viewSize;
@synthesize customButtons = _customButtons;
@synthesize hideCancel = _hideCancel;
-#pragma mark -
-#pragma mark NSObject
-
+#pragma mark - Abstract Implementation
-+ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target action:(SEL)action origin:(id)origin {
- ActionSheetPicker *picker = [[[ActionSheetPicker alloc] initWithTitle:title rows:data initialSelection:index target:target action:action origin:origin] autorelease];
- [picker showActionSheetPicker];
- return picker;
-}
-
-- (ActionSheetPicker *)initWithTarget:(id)target action:(SEL)action origin:(id)origin {
- NSParameterAssert( (origin != NULL) && (target != NULL) );
+- (id)initWithTarget:(id)target successAction:(SEL)successAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin {
+ NSParameterAssert( (origin != NULL) );
self = [super init];
if (self) {
self.target = target;
- self.action = action;
+ self.successAction = successAction;
+ self.cancelAction = cancelActionOrNil;
if ([origin isKindOfClass:[UIBarButtonItem class]])
self.barButtonItem = origin;
else if ([origin isKindOfClass:[UIView class]])
@@ -103,56 +90,36 @@ - (ActionSheetPicker *)initWithTarget:(id)target action:(SEL)action origin:(id)o
return self;
}
-- (id)initWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target action:(SEL)action origin:(id)origin {
- NSParameterAssert( (origin != NULL) && (target != NULL) );
- self = [self initWithTarget:target action:action origin:origin];
- if (self) {
- self.data = data;
- self.selectedIndex = index;
- self.title = title;
- }
- return self;
-}
-
- (void)dealloc {
self.actionSheet = nil;
self.popOverController = nil;
- self.data = nil;
self.customButtons = nil;
self.pickerView = nil;
self.containerView = nil;
self.barButtonItem = nil;
self.target = nil;
+ // should these two be included?
+ self.successAction = nil;
+ self.cancelAction = nil;
[super dealloc];
}
-#pragma mark -
-#pragma mark Implementation
-
- (UIView *)configuredPickerView {
- if (!self.data)
- return nil;
- CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
- UIPickerView *stringPicker = [[[UIPickerView alloc] initWithFrame:pickerFrame] autorelease];
- stringPicker.delegate = self;
- stringPicker.dataSource = self;
- stringPicker.showsSelectionIndicator = YES;
- [stringPicker selectRow:self.selectedIndex inComponent:0 animated:NO];
- return stringPicker;
+ NSAssert(NO, @"This is an abstract class, you must use a subclass of ActionSheetPicker (like ActionSheetStringPicker)");
+ return nil;
}
+- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)sucessAction origin:(id)origin {
+ NSAssert(NO, @"This is an abstract class, you must use a subclass of ActionSheetPicker (like ActionSheetStringPicker)");
+}
-- (void)addCustomButtonWithTitle:(NSString *)title value:(id)value {
- NSArray *customButtonArray = [[NSArray alloc] initWithObjects:title, value, nil];
-
- if (nil == self.customButtons)
- _customButtons = [[NSMutableArray alloc] init];
-
- [self.customButtons addObject:customButtonArray];
- [customButtonArray release];
-
+- (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin {
+ if (target && cancelAction && [target respondsToSelector:cancelAction])
+ [target performSelector:cancelAction withObject:origin];
}
+#pragma mark - Actions
+
- (void)showActionSheetPicker {
UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];
UIToolbar *pickerToolbar = [self createPickerToolbarWithTitle:self.title];
@@ -165,134 +132,55 @@ - (void)showActionSheetPicker {
[masterView release];
}
- // subclasses should override this for custom behavior
-- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)origin {
-
- if ([target respondsToSelector:action])
- [target performSelector:action withObject:[NSNumber numberWithInt:self.selectedIndex] withObject:origin];
- else
- NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);
-
- //notify delegate
- if (nil != self.delegate && [self.delegate respondsToSelector:@selector(actionPickerDoneWithValue:)])
- [self.delegate actionPickerDoneWithValue:[self.data objectAtIndex:self.selectedIndex]];
-
-}
-
-#pragma mark - String Picker Data Source
-
-- (CGSize)viewSize {
- if (![self isViewPortrait])
- return CGSizeMake(480, 320);
- return CGSizeMake(320, 480);
-}
-
-- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- self.selectedIndex = row;
-}
-
-- (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];
-}
-
-- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
- return pickerView.frame.size.width - 30;
-}
-
-#pragma mark - Popovers and ActionSheets
-
-- (void)presentPickerForView:(UIView *)aView {
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- [self configureAndPresentPopoverForView:aView];
- else
- [self configureAndPresentActionSheetForView:aView];
-}
-
-- (void)configureAndPresentActionSheetForView:(UIView *)aView {
- NSString *paddedSheetTitle = nil;
- if ([self isViewPortrait])
- paddedSheetTitle = @"\n\n\n"; // looks hacky to me
- _actionSheet = [[UIActionSheet alloc] initWithTitle:paddedSheetTitle delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
- [_actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
- [_actionSheet addSubview:aView];
- [self presentActionSheet:_actionSheet];
- _actionSheet.bounds = CGRectMake(0, 0, self.viewSize.width, self.viewSize.height+5);
-}
-
-- (void)presentActionSheet:(UIActionSheet *)actionSheet {
- NSParameterAssert(actionSheet != NULL);
- if (self.barButtonItem)
- [actionSheet showFromBarButtonItem:_barButtonItem animated:YES];
- else if (self.containerView)
- [actionSheet showInView:_containerView];
-}
-
-- (void)configureAndPresentPopoverForView:(UIView *)aView {
- UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
- viewController.view = aView;
- viewController.contentSizeForViewInPopover = viewController.view.frame.size;
- _popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
- [self presentPopover:_popOverController];
- [viewController release];
-}
-
-- (void)presentPopover:(UIPopoverController *)popover {
- NSParameterAssert(popover != NULL);
- if (self.barButtonItem)
- [popover presentPopoverFromBarButtonItem:_barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
- else if (self.containerView) {
- UIView *popoverContents = (_containerView.superview ? _containerView.superview : _containerView);
- [popover presentPopoverFromRect:_containerView.frame inView:popoverContents permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
- }
-}
-
-#pragma mark - Convenient Utilities
-
-- (void)actionPickerDone:(id)sender {
- NSAssert(self.target != NULL, @"Cannot perform an action on a null target");
- NSAssert(self.action != NULL, @"Cannot perform a null action");
- [self notifyTarget:self.target didSucceedWithAction:self.action origin:[self storedOrigin]];
-
+- (IBAction)actionPickerDone:(id)sender {
+ [self notifyTarget:self.target didSucceedWithAction:self.successAction origin:[self storedOrigin]];
[self dismissPicker];
}
-- (void)actionPickerCancel:(id)sender {
- if (nil != self.delegate)
- [self.delegate actionPickerCancelled];
-
+- (IBAction)actionPickerCancel:(id)sender {
+ [self notifyTarget:self.target didCancelWithAction:self.cancelAction origin:[self storedOrigin]];
[self dismissPicker];
}
- (void)dismissPicker {
- if (self.actionSheet)
+ if (self.actionSheet && self.actionSheet.visible)
[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
- else if (self.popOverController)
+ else if (self.popOverController && self.popOverController.popoverVisible)
[_popOverController dismissPopoverAnimated:YES];
+ self.actionSheet = nil;
+ self.popOverController = nil;
self.selfReference = nil;
}
-- (void)customButtonPressed:(id)sender {
+#pragma mark - Custom Buttons
+
+- (void)addCustomButtonWithTitle:(NSString *)title value:(id)value {
+ if (!self.customButtons)
+ _customButtons = [[NSMutableArray alloc] init];
+ if (!title)
+ title = @"";
+ if (!value)
+ value = [NSNumber numberWithInt:0];
+ NSDictionary *buttonDetails = [[NSDictionary alloc] initWithObjectsAndKeys:title, @"buttonTitle", value, @"buttonValue", nil];
+ [self.customButtons addObject:buttonDetails];
+ [buttonDetails release];
+}
+
+- (IBAction)customButtonPressed:(id)sender {
UIBarButtonItem *button = (UIBarButtonItem*)sender;
NSInteger index = button.tag;
-
- NSAssert((0 <= index && index < self.customButtons.count), @"Bad custom button tag: %d, custom button count: %d", index, self.customButtons.count);
+ NSAssert((index >= 0 && index < self.customButtons.count), @"Bad custom button tag: %d, custom button count: %d", index, self.customButtons.count);
NSAssert([self.pickerView respondsToSelector:@selector(selectRow:inComponent:animated:)], @"customButtonPressed not overridden, cannot interact with subclassed pickerView");
-
- //retrieve custom button's associated value index
- NSInteger itemValue = [[[self.customButtons objectAtIndex:index] objectAtIndex:1] intValue];
-
+ NSDictionary *buttonDetails = [self.customButtons objectAtIndex:index];
+ NSAssert(buttonDetails != NULL, @"Custom button dictionary is invalid");
+ NSInteger buttonValue = [[buttonDetails objectForKey:@"buttonValue"] intValue];
UIPickerView *picker = (UIPickerView *)self.pickerView;
-
- [picker selectRow:itemValue inComponent:0 animated:YES];
- [self pickerView:picker didSelectRow:itemValue inComponent:0];
+ NSAssert(picker != NULL, @"PickerView is invalid");
+ [picker selectRow:buttonValue inComponent:0 animated:YES];
+ if ([self respondsToSelector:@selector(pickerView:didSelectRow:inComponent:)]) {
+ void (*objc_msgSendTyped)(id self, SEL _cmd, id pickerView, NSInteger row, NSInteger component) = (void*)objc_msgSend; // sending Integers as params
+ objc_msgSendTyped(self, @selector(pickerView:didSelectRow:inComponent:), picker, buttonValue, 0);
+ }
}
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {
@@ -300,40 +188,29 @@ - (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {
UIToolbar *pickerToolbar = [[[UIToolbar alloc] initWithFrame:frame] autorelease];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
-
- /* custom buttons */
- if (nil != self.customButtons) {
- for (NSInteger i = 0; i < self.customButtons.count; i++) {
- //create the button
- UIBarButtonItem *customButton = [[[UIBarButtonItem alloc] initWithTitle:[[self.customButtons objectAtIndex:i] objectAtIndex:0]
- style:UIBarButtonItemStyleBordered
- target:self
- action:@selector(customButtonPressed:)] autorelease];
- //record associated customButtons index
- customButton.tag = i;
-
- //add to button list
- [barItems addObject:customButton];
- }
+ NSInteger index = 0;
+ for (NSDictionary *buttonDetails in self.customButtons) {
+ NSString *buttonTitle = [buttonDetails objectForKey:@"buttonTitle"];
+ //NSInteger buttonValue = [[buttonDetails objectForKey:@"buttonValue"] intValue];
+ UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:buttonTitle style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonPressed:)];
+ button.tag = index;
+ [barItems addObject:button];
+ [button release];
+ index++;
}
-
if (NO == self.hideCancel) {
UIBarButtonItem *cancelBtn = [self createButtonWithType:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)];
[barItems addObject:cancelBtn];
}
-
UIBarButtonItem *flexSpace = [self createButtonWithType:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
-
if (title){
UIBarButtonItem *labelButton = [self createToolbarLabelWithTitle:title];
[barItems addObject:labelButton];
[barItems addObject:flexSpace];
}
-
UIBarButtonItem *doneButton = [self createButtonWithType:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)];
[barItems addObject:doneButton];
-
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
return pickerToolbar;
@@ -355,6 +232,14 @@ - (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id
return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction] autorelease];
}
+#pragma mark - Utilities and Accessors
+
+- (CGSize)viewSize {
+ if (![self isViewPortrait])
+ return CGSizeMake(480, 320);
+ return CGSizeMake(320, 480);
+}
+
- (BOOL)isViewPortrait {
return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}
@@ -373,5 +258,52 @@ - (id)storedOrigin {
return self.containerView;
}
+#pragma mark - Popovers and ActionSheets
+
+- (void)presentPickerForView:(UIView *)aView {
+ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
+ [self configureAndPresentPopoverForView:aView];
+ else
+ [self configureAndPresentActionSheetForView:aView];
+}
+
+- (void)configureAndPresentActionSheetForView:(UIView *)aView {
+ NSString *paddedSheetTitle = nil;
+ if ([self isViewPortrait])
+ paddedSheetTitle = @"\n\n\n"; // looks hacky to me
+ _actionSheet = [[UIActionSheet alloc] initWithTitle:paddedSheetTitle delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
+ [_actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
+ [_actionSheet addSubview:aView];
+ [self presentActionSheet:_actionSheet];
+ _actionSheet.bounds = CGRectMake(0, 0, self.viewSize.width, self.viewSize.height+5);
+}
+
+- (void)presentActionSheet:(UIActionSheet *)actionSheet {
+ NSParameterAssert(actionSheet != NULL);
+ if (self.barButtonItem)
+ [actionSheet showFromBarButtonItem:_barButtonItem animated:YES];
+ else if (self.containerView)
+ [actionSheet showInView:_containerView];
+}
+
+- (void)configureAndPresentPopoverForView:(UIView *)aView {
+ UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
+ viewController.view = aView;
+ viewController.contentSizeForViewInPopover = viewController.view.frame.size;
+ _popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
+ [self presentPopover:_popOverController];
+ [viewController release];
+}
+
+- (void)presentPopover:(UIPopoverController *)popover {
+ NSParameterAssert(popover != NULL);
+ if (self.barButtonItem)
+ [popover presentPopoverFromBarButtonItem:_barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
+ else if (self.containerView) {
+ UIView *popoverContents = (_containerView.superview ? _containerView.superview : _containerView);
+ [popover presentPopoverFromRect:_containerView.frame inView:popoverContents permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
+ }
+}
+
@end
View
12 Pickers/ActionSheetDatePicker.h
@@ -25,17 +25,13 @@
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
-#import "ActionSheetPicker.h"
+#import "AbstractActionSheetPicker.h"
-@interface ActionSheetDatePicker : ActionSheetPicker
+@interface ActionSheetDatePicker : AbstractActionSheetPicker
-+ (id)showPickerWithTitle:(NSString *)title
- datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
- target:(id)target action:(SEL)action origin:(id)origin;
++ (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin;
-- (id)initWithTitle:(NSString *)title
- datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
- target:(id)target action:(SEL)action origin:(id)origin;
+- (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin;
- (void)eventForDatePicker:(id)sender;
View
36 Pickers/ActionSheetDatePicker.m
@@ -27,7 +27,6 @@
#import "ActionSheetDatePicker.h"
-
#import <objc/message.h>
@interface ActionSheetDatePicker()
@@ -42,18 +41,15 @@ @implementation ActionSheetDatePicker
+ (id)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
target:(id)target action:(SEL)action origin:(id)origin {
- ActionSheetDatePicker *picker = [[[ActionSheetDatePicker alloc] initWithTitle:title
- datePickerMode:datePickerMode selectedDate:selectedDate
- target:target action:action origin:origin] autorelease];
+ ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:title datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action origin:origin];
[picker showActionSheetPicker];
- return picker;
+ return [picker autorelease];
}
-- (id)initWithTitle:(NSString *)title
- datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
- target:(id)target action:(SEL)action origin:(id)origin {
- self = [super initWithTitle:title rows:nil initialSelection:0 target:target action:action origin:origin];
+- (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin {
+ self = [super initWithTarget:target successAction:action cancelAction:nil origin:origin];
if (self) {
+ self.title = title;
self.datePickerMode = datePickerMode;
self.selectedDate = selectedDate;
}
@@ -79,31 +75,23 @@ - (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)orig
objc_msgSend(target, action, self.selectedDate, origin);
else
NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);
-
- //notify delegate
- if (nil != self.delegate && [self.delegate respondsToSelector:@selector(actionPickerDoneWithValue:)])
- [self.delegate actionPickerDoneWithValue:self.selectedDate];
}
- (void)eventForDatePicker:(id)sender {
- if (sender && [sender isKindOfClass:[UIDatePicker class]]) {
- UIDatePicker *datePicker = (UIDatePicker *)sender;
- self.selectedDate = datePicker.date;
- }
+ if (!sender || ![sender isKindOfClass:[UIDatePicker class]])
+ return;
+ UIDatePicker *datePicker = (UIDatePicker *)sender;
+ self.selectedDate = datePicker.date;
}
-
- (void)customButtonPressed:(id)sender {
UIBarButtonItem *button = (UIBarButtonItem*)sender;
NSInteger index = button.tag;
-
- NSAssert((0 <= index && index < self.customButtons.count), @"Bad custom button tag: %d, custom button count: %d", index, self.customButtons.count);
+ NSAssert((index >= 0 && index < self.customButtons.count), @"Bad custom button tag: %d, custom button count: %d", index, self.customButtons.count);
NSAssert([self.pickerView respondsToSelector:@selector(setDate:animated:)], @"Bad pickerView for ActionSheetDatePicker, doesn't respond to setDate:animated:");
-
- //retrieve custom button's associated value index
- NSDate *itemValue = [[self.customButtons objectAtIndex:index] objectAtIndex:1];
+ NSDictionary *buttonDetails = [self.customButtons objectAtIndex:index];
+ NSDate *itemValue = [buttonDetails objectForKey:@"buttonValue"];
UIDatePicker *picker = (UIDatePicker *)self.pickerView;
-
[picker setDate:itemValue animated:YES];
[self eventForDatePicker:picker];
}
View
14 Pickers/ActionSheetDistancePicker.h
@@ -25,18 +25,12 @@
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
-#import "ActionSheetPicker.h"
+#import "AbstractActionSheetPicker.h"
#import "DistancePickerView.h"
-@interface ActionSheetDistancePicker : ActionSheetPicker
+@interface ActionSheetDistancePicker : AbstractActionSheetPicker <UIPickerViewDelegate, UIPickerViewDataSource>
-+ (id)showPickerWithTitle:(NSString *)title
- bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit
- smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit
- target:(id)target action:(SEL)action origin:(id)origin;
++ (id)showPickerWithTitle:(NSString *)title bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit target:(id)target action:(SEL)action origin:(id)origin;
-- (id)initWithTitle:(NSString *)title
- bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit
- smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit
- target:(id)target action:(SEL)action origin:(id)origin;
+- (id)initWithTitle:(NSString *)title bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit target:(id)target action:(SEL)action origin:(id)origin;
@end
View
33 Pickers/ActionSheetDistancePicker.m
@@ -26,7 +26,6 @@
//
#import "ActionSheetDistancePicker.h"
-
#import <objc/message.h>
@interface ActionSheetDistancePicker()
@@ -50,27 +49,16 @@ @implementation ActionSheetDistancePicker
@synthesize smallUnitDigits = _smallUnitDigits;
@synthesize selectedSmallUnit = _selectedSmallUnit;
-+ (id)showPickerWithTitle:(NSString *)title
- bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit
- smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit
- target:(id)target action:(SEL)action origin:(id)origin {
- ActionSheetDistancePicker *picker = [[[ActionSheetDistancePicker alloc] initWithTitle:title
- bigUnitString:bigUnitString bigUnitMax:bigUnitMax selectedBigUnit:selectedBigUnit
- smallUnitString:smallUnitString smallUnitMax:smallUnitMax selectedSmallUnit:selectedSmallUnit
- target:target action:action origin:origin] autorelease];
++ (id)showPickerWithTitle:(NSString *)title bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit target:(id)target action:(SEL)action origin:(id)origin {
+ ActionSheetDistancePicker *picker = [[ActionSheetDistancePicker alloc] initWithTitle:title bigUnitString:bigUnitString bigUnitMax:bigUnitMax selectedBigUnit:selectedBigUnit smallUnitString:smallUnitString smallUnitMax:smallUnitMax selectedSmallUnit:selectedSmallUnit target:target action:action origin:origin];
[picker showActionSheetPicker];
- return picker;
+ return [picker autorelease];
}
-- (id)initWithTitle:(NSString *)title
- bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit
- smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit
- target:(id)target
- action:(SEL)action
- origin:(id)origin {
-
- self = [super initWithTitle:title rows:nil initialSelection:0 target:target action:action origin:origin];
+- (id)initWithTitle:(NSString *)title bigUnitString:(NSString *)bigUnitString bigUnitMax:(NSInteger)bigUnitMax selectedBigUnit:(NSInteger)selectedBigUnit smallUnitString:(NSString*)smallUnitString smallUnitMax:(NSInteger)smallUnitMax selectedSmallUnit:(NSInteger)selectedSmallUnit target:(id)target action:(SEL)action origin:(id)origin {
+ self = [super initWithTarget:target successAction:action cancelAction:nil origin:origin];
if (self) {
+ self.title = title;
self.bigUnitString = bigUnitString;
self.bigUnitMax = bigUnitMax;
self.selectedBigUnit = selectedBigUnit;
@@ -134,11 +122,6 @@ - (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)orig
objc_msgSend(target, action, [NSNumber numberWithInt:bigUnits], [NSNumber numberWithInt:smallUnits], origin);
else
NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);
-
- //notify delegate
- if (nil != self.delegate && [self.delegate respondsToSelector:@selector(actionPickerDoneWithValue:)])
- [self.delegate actionPickerDoneWithValue:[NSArray arrayWithObjects:[NSNumber numberWithInt:bigUnits], [NSNumber numberWithInt:smallUnits], nil]];
-
}
#pragma mark -
@@ -164,7 +147,7 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
- CGFloat totalWidth = [super pickerView:pickerView widthForComponent:component];
+ CGFloat totalWidth = pickerView.frame.size.width - 30;
CGFloat bigUnitLabelSize = [self.bigUnitString sizeWithFont:[UIFont boldSystemFontOfSize:20]].width;
CGFloat smallUnitLabelSize = [self.smallUnitString sizeWithFont:[UIFont boldSystemFontOfSize:20]].width;
CGFloat otherSize = (totalWidth - bigUnitLabelSize - smallUnitLabelSize)/(self.bigUnitDigits + self.smallUnitDigits);
@@ -177,7 +160,7 @@ - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)co
- (void)customButtonPressed:(id)sender {
- NSLog(@"Not implemented. If you get around to it, please contribute back to the projcet :)");
+ NSLog(@"Not implemented. If you get around to it, please contribute back to the project :)");
}
@end
View
55 Pickers/ActionSheetStringPicker.h
@@ -0,0 +1,55 @@
+//
+//Copyright (c) 2011, Tim Cinel
+//All rights reserved.
+//
+//Redistribution and use in source and binary forms, with or without
+//modification, are permitted provided that the following conditions are met:
+//* Redistributions of source code must retain the above copyright
+//notice, this list of conditions and the following disclaimer.
+//* Redistributions in binary form must reproduce the above copyright
+//notice, this list of conditions and the following disclaimer in the
+//documentation and/or other materials provided with the distribution.
+//* Neither the name of the <organization> nor the
+//names of its contributors may be used to endorse or promote products
+//derived from this software without specific prior written permission.
+//
+//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+//åLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "AbstractActionSheetPicker.h"
+
+@class ActionSheetStringPicker;
+typedef void(^ActionStringDoneBlock)(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue);
+typedef void(^ActionStringCancelBlock)(ActionSheetStringPicker *picker);
+
+@interface ActionSheetStringPicker : AbstractActionSheetPicker <UIPickerViewDelegate, UIPickerViewDataSource>
+/* Create and display an action sheet picker. The returned picker is autoreleased.
+ "origin" must not be empty. It can be either an originating container view or a UIBarButtonItem to use with a popover arrow.
+ "target" must not be empty. It should respond to "onSuccess" actions.
+ "rows" is an array of strings to use for the picker's available selection choices.
+ "initialSelection" is used to establish the initially selected row;
+ */
++ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target sucessAction:(SEL)sucessAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin;
+
+ // Create an action sheet picker, but don't display until a subsequent call to "showActionPicker". Receiver must release the picker when ready. */
+- (id)initWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target sucessAction:(SEL)sucessAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin;
+
+
+
++ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)strings initialSelection:(NSInteger)index doneBlock:(ActionStringDoneBlock)doneBlock cancelBlock:(ActionStringCancelBlock)cancelBlock origin:(id)origin;
+
+- (id)initWithTitle:(NSString *)title rows:(NSArray *)strings initialSelection:(NSInteger)index doneBlock:(ActionStringDoneBlock)doneBlock cancelBlock:(ActionStringCancelBlock)cancelBlockOrNil origin:(id)origin;
+
+@property (nonatomic, copy) ActionStringDoneBlock onActionSheetDone;
+@property (nonatomic, copy) ActionStringCancelBlock onActionSheetCancel;
+
+@end
View
178 Pickers/ActionSheetStringPicker.m
@@ -0,0 +1,178 @@
+//
+//Copyright (c) 2011, Tim Cinel
+//All rights reserved.
+//
+//Redistribution and use in source and binary forms, with or without
+//modification, are permitted provided that the following conditions are met:
+//* Redistributions of source code must retain the above copyright
+//notice, this list of conditions and the following disclaimer.
+//* Redistributions in binary form must reproduce the above copyright
+//notice, this list of conditions and the following disclaimer in the
+//documentation and/or other materials provided with the distribution.
+//* Neither the name of the <organization> nor the
+//names of its contributors may be used to endorse or promote products
+//derived from this software without specific prior written permission.
+//
+//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+//åLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "ActionSheetStringPicker.h"
+/*
+
+
+- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)origin {
+ if (self.onActionSheetDone)
+ _onActionSheetDone(self, self.selectedIndex, [self.data objectAtIndex:self.selectedIndex]);
+ if ([target respondsToSelector:action])
+ [target performSelector:action withObject:[NSNumber numberWithInt:self.selectedIndex] withObject:origin];
+ else
+ NSAssert(NO, @"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)action);
+
+ //notify delegate
+ if (nil != self.delegate && [self.delegate respondsToSelector:@selector(actionPickerDoneWithValue:)])
+ [self.delegate actionPickerDoneWithValue:[self.data objectAtIndex:self.selectedIndex]];
+
+}
+
+
+if (nil != self.delegate && [self.delegate respondsToSelector:@selector(actionPickerDoneWithValue:)])
+[self.delegate actionPickerDoneWithValue:[self.data objectAtIndex:self.selectedIndex]];
+
+@end
+*/
+
+@interface ActionSheetStringPicker()
+@property (nonatomic,retain) NSArray *data;
+@property (nonatomic,assign) NSInteger selectedIndex;
+@end
+
+@implementation ActionSheetStringPicker
+@synthesize data = _data;
+@synthesize selectedIndex = _selectedIndex;
+@synthesize onActionSheetDone = _onActionSheetDone;
+@synthesize onActionSheetCancel = _onActionSheetCancel;
+
++ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)strings initialSelection:(NSInteger)index doneBlock:(ActionStringDoneBlock)doneBlock cancelBlock:(ActionStringCancelBlock)cancelBlockOrNil origin:(id)origin {
+ ActionSheetStringPicker * picker = [[ActionSheetStringPicker alloc] initWithTitle:title rows:strings initialSelection:index doneBlock:doneBlock cancelBlock:cancelBlockOrNil origin:origin];
+ [picker showActionSheetPicker];
+ return [picker autorelease];
+}
+
+- (id)initWithTitle:(NSString *)title rows:(NSArray *)strings initialSelection:(NSInteger)index doneBlock:(ActionStringDoneBlock)doneBlock cancelBlock:(ActionStringCancelBlock)cancelBlockOrNil origin:(id)origin {
+ self = [self initWithTitle:title rows:strings initialSelection:index target:nil sucessAction:nil cancelAction:nil origin:origin];
+ if (self) {
+ self.onActionSheetDone = doneBlock;
+ self.onActionSheetCancel = cancelBlockOrNil;
+ }
+ return self;
+}
+
++ (id)showPickerWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target sucessAction:(SEL)sucessAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin {
+ ActionSheetStringPicker *picker = [[[ActionSheetStringPicker alloc] initWithTitle:title rows:data initialSelection:index target:target sucessAction:sucessAction cancelAction:cancelActionOrNil origin:origin] autorelease];
+ [picker showActionSheetPicker];
+ return picker;
+}
+
+- (id)initWithTitle:(NSString *)title rows:(NSArray *)data initialSelection:(NSInteger)index target:(id)target sucessAction:(SEL)sucessAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin {
+ NSParameterAssert( (origin != NULL) );
+ self = [self initWithTarget:target successAction:sucessAction cancelAction:cancelActionOrNil origin:origin];
+ if (self) {
+ self.data = data;
+ self.selectedIndex = index;
+ self.title = title;
+ }
+ return self;
+}
+
+- (void)dealloc {
+ self.data = nil;
+ Block_release(_onActionSheetDone);
+ Block_release(_onActionSheetCancel);
+ [super dealloc];
+}
+
+- (UIView *)configuredPickerView {
+ if (!self.data)
+ return nil;
+ CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
+ UIPickerView *stringPicker = [[[UIPickerView alloc] initWithFrame:pickerFrame] autorelease];
+ stringPicker.delegate = self;
+ stringPicker.dataSource = self;
+ stringPicker.showsSelectionIndicator = YES;
+ [stringPicker selectRow:self.selectedIndex inComponent:0 animated:NO];
+ return stringPicker;
+}
+
+- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)sucessAction origin:(id)origin {
+ if (self.onActionSheetDone) {
+ _onActionSheetDone(self, self.selectedIndex, [self.data objectAtIndex:self.selectedIndex]);
+ return;
+ }
+ else if (target && [target respondsToSelector:sucessAction]) {
+ [target performSelector:sucessAction withObject:[NSNumber numberWithInt:self.selectedIndex] withObject:origin];
+ return;
+ }
+ NSLog(@"Invalid target/action ( %s / %s ) combination used for ActionSheetPicker", object_getClassName(target), (char *)sucessAction);
+}
+
+- (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin {
+ if (self.onActionSheetCancel) {
+ _onActionSheetCancel(self);
+ return;
+ }
+ else if (target && cancelAction && [target respondsToSelector:cancelAction])
+ [target performSelector:cancelAction withObject:origin];
+}
+
+#pragma mark - UIPickerViewDelegate / DataSource
+
+- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
+ self.selectedIndex = row;
+}
+
+- (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];
+}
+
+- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
+ return pickerView.frame.size.width - 30;
+}
+
+#pragma mark - Block setters
+
+ // NOTE: Sometimes see crashes when relying on just the copy property. Using Block_copy ensures correct behavior
+
+- (void)setOnActionSheetDone:(ActionStringDoneBlock)onActionSheetDone {
+ if (_onActionSheetDone) {
+ Block_release(_onActionSheetDone);
+ _onActionSheetDone = nil;
+ }
+ _onActionSheetDone = Block_copy(onActionSheetDone);
+}
+
+- (void)setOnActionSheetCancel:(ActionStringCancelBlock)onActionSheetCancel {
+ if (_onActionSheetCancel) {
+ Block_release(_onActionSheetCancel);
+ _onActionSheetCancel = nil;
+ }
+ _onActionSheetCancel = Block_copy(onActionSheetCancel);
+}
+
+@end
View
20 Sample/ActionSheetPicker.xcodeproj/project.pbxproj
@@ -13,13 +13,14 @@
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
2237C38B146FBABC00C74831 /* ActionSheetDatePicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 2237C388146FBABC00C74831 /* ActionSheetDatePicker.m */; };
2237C38C146FBABC00C74831 /* ActionSheetDistancePicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 2237C38A146FBABC00C74831 /* ActionSheetDistancePicker.m */; };
- 2237C38F146FBAF800C74831 /* ActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 2237C38E146FBAF800C74831 /* ActionSheetPicker.m */; };
2237C392146FBC3000C74831 /* DistancePickerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 2237C391146FBC3000C74831 /* DistancePickerView.m */; };
2237C3971470D1F300C74831 /* NSDate+TCUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 2237C3961470D1F300C74831 /* NSDate+TCUtils.m */; };
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; };
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 */; };
+ 37037553147B387900B59AA2 /* ActionSheetStringPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 37037552147B387900B59AA2 /* ActionSheetStringPicker.m */; };
+ 37037557147B73F000B59AA2 /* AbstractActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 37037556147B73EE00B59AA2 /* AbstractActionSheetPicker.m */; };
52CFA62313A4D6FA0022667F /* MainWindow-iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = 52CFA62213A4D6FA0022667F /* MainWindow-iPad.xib */; };
52FDE22813A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib in Resources */ = {isa = PBXBuildFile; fileRef = 52FDE22713A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib */; };
/* End PBXBuildFile section */
@@ -34,8 +35,6 @@
2237C388146FBABC00C74831 /* ActionSheetDatePicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetDatePicker.m; path = ../../Pickers/ActionSheetDatePicker.m; sourceTree = "<group>"; };
2237C389146FBABC00C74831 /* ActionSheetDistancePicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetDistancePicker.h; path = ../../Pickers/ActionSheetDistancePicker.h; sourceTree = "<group>"; };
2237C38A146FBABC00C74831 /* ActionSheetDistancePicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetDistancePicker.m; path = ../../Pickers/ActionSheetDistancePicker.m; sourceTree = "<group>"; };
- 2237C38D146FBAF800C74831 /* ActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetPicker.h; path = ../../ActionSheetPicker.h; sourceTree = "<group>"; };
- 2237C38E146FBAF800C74831 /* ActionSheetPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetPicker.m; path = ../../ActionSheetPicker.m; sourceTree = "<group>"; };
2237C390146FBC3000C74831 /* DistancePickerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DistancePickerView.h; path = ../../Pickers/DistancePickerView.h; sourceTree = "<group>"; };
2237C391146FBC3000C74831 /* DistancePickerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DistancePickerView.m; path = ../../Pickers/DistancePickerView.m; sourceTree = "<group>"; };
2237C3951470D1F300C74831 /* NSDate+TCUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDate+TCUtils.h"; sourceTree = "<group>"; };
@@ -47,6 +46,11 @@
28D7ACF70DDB3853001CB0EB /* ActionSheetPickerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActionSheetPickerViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* ActionSheetPicker_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionSheetPicker_Prefix.pch; sourceTree = "<group>"; };
+ 37037551147B387900B59AA2 /* ActionSheetStringPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetStringPicker.h; path = ../../Pickers/ActionSheetStringPicker.h; sourceTree = "<group>"; };
+ 37037552147B387900B59AA2 /* ActionSheetStringPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ActionSheetStringPicker.m; path = ../../Pickers/ActionSheetStringPicker.m; sourceTree = "<group>"; };
+ 37037555147B73EE00B59AA2 /* AbstractActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AbstractActionSheetPicker.h; path = ../../Pickers/AbstractActionSheetPicker.h; sourceTree = "<group>"; };
+ 37037556147B73EE00B59AA2 /* AbstractActionSheetPicker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AbstractActionSheetPicker.m; path = ../../Pickers/AbstractActionSheetPicker.m; sourceTree = "<group>"; };
+ 37037559147B7EC000B59AA2 /* ActionSheetPicker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ActionSheetPicker.h; path = ../../ActionSheetPicker.h; sourceTree = "<group>"; };
52CFA62213A4D6FA0022667F /* MainWindow-iPad.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = "MainWindow-iPad.xib"; path = "iPad/MainWindow-iPad.xib"; sourceTree = "<group>"; };
52FDE22713A51D2600D8D2C0 /* ActionSheetPickerViewController-iPad.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = "ActionSheetPickerViewController-iPad.xib"; sourceTree = "<group>"; };
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 = "<group>"; };
@@ -90,6 +94,10 @@
2237C393146FC07400C74831 /* Pickers */ = {
isa = PBXGroup;
children = (
+ 37037555147B73EE00B59AA2 /* AbstractActionSheetPicker.h */,
+ 37037556147B73EE00B59AA2 /* AbstractActionSheetPicker.m */,
+ 37037551147B387900B59AA2 /* ActionSheetStringPicker.h */,
+ 37037552147B387900B59AA2 /* ActionSheetStringPicker.m */,
2237C387146FBABC00C74831 /* ActionSheetDatePicker.h */,
2237C388146FBABC00C74831 /* ActionSheetDatePicker.m */,
2237C389146FBABC00C74831 /* ActionSheetDistancePicker.h */,
@@ -103,8 +111,7 @@
2237C394146FC09B00C74831 /* ActionSheetPicker */ = {
isa = PBXGroup;
children = (
- 2237C38D146FBAF800C74831 /* ActionSheetPicker.h */,
- 2237C38E146FBAF800C74831 /* ActionSheetPicker.m */,
+ 37037559147B7EC000B59AA2 /* ActionSheetPicker.h */,
2237C393146FC07400C74831 /* Pickers */,
);
name = ActionSheetPicker;
@@ -241,9 +248,10 @@
28D7ACF80DDB3853001CB0EB /* ActionSheetPickerViewController.m in Sources */,
2237C38B146FBABC00C74831 /* ActionSheetDatePicker.m in Sources */,
2237C38C146FBABC00C74831 /* ActionSheetDistancePicker.m in Sources */,
- 2237C38F146FBAF800C74831 /* ActionSheetPicker.m in Sources */,
2237C392146FBC3000C74831 /* DistancePickerView.m in Sources */,
2237C3971470D1F300C74831 /* NSDate+TCUtils.m in Sources */,
+ 37037553147B387900B59AA2 /* ActionSheetStringPicker.m in Sources */,
+ 37037557147B73F000B59AA2 /* AbstractActionSheetPicker.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
76 Sample/ActionSheetPickerViewController-iPad.xib
@@ -80,13 +80,39 @@
<int key="NSfFlags">16</int>
</object>
</object>
+ <object class="IBUITextField" id="239632134">
+ <reference key="NSNextResponder" ref="191373211"/>
+ <int key="NSvFlags">301</int>
+ <string key="NSFrame">{{244, 372}, {280, 31}}</string>
+ <reference key="NSSuperview" ref="191373211"/>
+ <reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="313250169"/>
+ <bool key="IBUIOpaque">NO</bool>
+ <bool key="IBUIClipsSubviews">YES</bool>
+ <string key="targetRuntimeIdentifier">IBIPadFramework</string>
+ <int key="IBUIContentVerticalAlignment">0</int>
+ <string key="IBUIText">Select a block...</string>
+ <int key="IBUIBorderStyle">3</int>
+ <object class="NSColor" key="IBUITextColor">
+ <int key="NSColorSpace">3</int>
+ <bytes key="NSWhite">MAA</bytes>
+ <reference key="NSCustomColorSpace" ref="305391248"/>
+ </object>
+ <bool key="IBUIAdjustsFontSizeToFit">YES</bool>
+ <float key="IBUIMinimumFontSize">17</float>
+ <object class="IBUITextInputTraits" key="IBUITextInputTraits">
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ </object>
+ <reference key="IBUIFontDescription" ref="497167348"/>
+ <reference key="IBUIFont" ref="276921910"/>
+ </object>
<object class="IBUITextField" id="525444202">
<reference key="NSNextResponder" ref="191373211"/>
<int key="NSvFlags">301</int>
<string key="NSFrame">{{244, 486}, {280, 31}}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="297430406"/>
+ <reference key="NSNextKeyView" ref="511984954"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<string key="targetRuntimeIdentifier">IBIPadFramework</string>
@@ -164,6 +190,7 @@
<string key="NSFrame">{{244, 963}, {280, 21}}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
+ <reference key="NSNextKeyView"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int>
@@ -183,7 +210,7 @@
<string key="NSFrameSize">{768, 44}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="313250169"/>
+ <reference key="NSNextKeyView" ref="239632134"/>
<string key="NSReuseIdentifierKey">_NS:255</string>
<string key="targetRuntimeIdentifier">IBIPadFramework</string>
<object class="NSArray" key="IBUIItems">
@@ -213,6 +240,7 @@
<string key="NSFrame">{{244, 541}, {280, 31}}</string>
<reference key="NSSuperview" ref="191373211"/>
<reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="297430406"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<string key="targetRuntimeIdentifier">IBIPadFramework</string>
@@ -268,12 +296,12 @@
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
- <string key="label">selectAnItem:</string>
+ <string key="label">selectAnAnimal:</string>
<reference key="source" ref="313250169"/>
<reference key="destination" ref="372490531"/>
<int key="IBEventType">1</int>
</object>
- <int key="connectionID">16</int>
+ <int key="connectionID">30</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
@@ -325,6 +353,23 @@
</object>
<int key="connectionID">26</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">delegate</string>
+ <reference key="source" ref="239632134"/>
+ <reference key="destination" ref="372490531"/>
+ </object>
+ <int key="connectionID">28</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchEventConnection" key="connection">
+ <string key="label">selectABlock:</string>
+ <reference key="source" ref="239632134"/>
+ <reference key="destination" ref="372490531"/>
+ <int key="IBEventType">1</int>
+ </object>
+ <int key="connectionID">31</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -349,6 +394,7 @@
<reference ref="313250169"/>
<reference ref="389164242"/>
<reference ref="511984954"/>
+ <reference ref="239632134"/>
</object>
<reference key="parent" ref="0"/>
</object>
@@ -422,6 +468,11 @@
<reference key="object" ref="511984954"/>
<reference key="parent" ref="191373211"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">27</int>
+ <reference key="object" ref="239632134"/>
+ <reference key="parent" ref="191373211"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -438,6 +489,7 @@
<string>20.IBPluginDependency</string>
<string>21.IBPluginDependency</string>
<string>24.IBPluginDependency</string>
+ <string>27.IBPluginDependency</string>
<string>3.IBPluginDependency</string>
<string>4.IBPluginDependency</string>
<string>6.IBPluginDependency</string>
@@ -461,6 +513,7 @@
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
+ <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
@@ -475,7 +528,7 @@
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
- <int key="maxID">26</int>
+ <int key="maxID">31</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -489,9 +542,10 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>animalButtonTapped:</string>
<string>dateButtonTapped:</string>
+ <string>selectABlock:</string>
<string>selectADate:</string>
<string>selectAMeasurement:</string>
- <string>selectAnItem:</string>
+ <string>selectAnAnimal:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -500,6 +554,7 @@
<string>id</string>
<string>id</string>
<string>id</string>
+ <string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
@@ -508,9 +563,10 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>animalButtonTapped:</string>
<string>dateButtonTapped:</string>
+ <string>selectABlock:</string>
<string>selectADate:</string>
<string>selectAMeasurement:</string>
- <string>selectAnItem:</string>
+ <string>selectAnAnimal:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -523,6 +579,10 @@
<string key="candidateClassName">UIBarButtonItem</string>
</object>
<object class="IBActionInfo">
+ <string key="name">selectABlock:</string>
+ <string key="candidateClassName">id</string>
+ </object>
+ <object class="IBActionInfo">
<string key="name">selectADate:</string>
<string key="candidateClassName">id</string>
</object>
@@ -531,7 +591,7 @@
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
- <string key="name">selectAnItem:</string>
+ <string key="name">selectAnAnimal:</string>
<string key="candidateClassName">id</string>
</object>
</object>
View
120 Sample/ActionSheetPickerViewController.xib
@@ -2,13 +2,13 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">1280</int>
- <string key="IBDocument.SystemVersion">10K540</string>
- <string key="IBDocument.InterfaceBuilderVersion">1891</string>
- <string key="IBDocument.AppKitVersion">1038.36</string>
- <string key="IBDocument.HIToolboxVersion">461.00</string>
+ <string key="IBDocument.SystemVersion">11C74</string>
+ <string key="IBDocument.InterfaceBuilderVersion">1938</string>
+ <string key="IBDocument.AppKitVersion">1138.23</string>
+ <string key="IBDocument.HIToolboxVersion">567.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
- <string key="NS.object.0">884</string>
+ <string key="NS.object.0">933</string>
</object>
<object class="NSArray" key="IBDocument.IntegratedClassDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -43,7 +43,7 @@
<object class="IBUITextField" id="506316691">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">293</int>
- <string key="NSFrame">{{20, 75}, {280, 31}}</string>
+ <string key="NSFrame">{{17, 149}, {280, 31}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="117506181"/>
@@ -80,7 +80,7 @@
<object class="IBUITextField" id="366061364">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">293</int>
- <string key="NSFrame">{{20, 185}, {280, 31}}</string>
+ <string key="NSFrame">{{17, 259}, {280, 31}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="1004339132"/>
@@ -106,7 +106,7 @@
<object class="IBUITextField" id="117506181">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">293</int>
- <string key="NSFrame">{{20, 130}, {280, 31}}</string>
+ <string key="NSFrame">{{17, 204}, {280, 31}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="366061364"/>
@@ -135,7 +135,7 @@
<string key="NSFrame">{{20, 20}, {280, 29}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="506316691"/>
+ <reference key="NSNextKeyView" ref="424092331"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int>
@@ -231,6 +231,32 @@
<reference key="IBUIFontDescription" ref="282050151"/>
<reference key="IBUIFont" ref="646051745"/>
</object>
+ <object class="IBUITextField" id="424092331">
+ <reference key="NSNextResponder" ref="774585933"/>
+ <int key="NSvFlags">293</int>
+ <string key="NSFrame">{{17, 96}, {280, 31}}</string>
+ <reference key="NSSuperview" ref="774585933"/>
+ <reference key="NSWindow"/>
+ <reference key="NSNextKeyView" ref="506316691"/>
+ <bool key="IBUIOpaque">NO</bool>
+ <bool key="IBUIClipsSubviews">YES</bool>
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ <int key="IBUIContentVerticalAlignment">0</int>
+ <string key="IBUIText">Select a block...</string>
+ <int key="IBUIBorderStyle">3</int>
+ <object class="NSColor" key="IBUITextColor">
+ <int key="NSColorSpace">3</int>
+ <bytes key="NSWhite">MAA</bytes>
+ <reference key="NSCustomColorSpace" ref="411068088"/>
+ </object>
+ <bool key="IBUIAdjustsFontSizeToFit">YES</bool>
+ <float key="IBUIMinimumFontSize">17</float>
+ <object class="IBUITextInputTraits" key="IBUITextInputTraits">
+ <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
+ </object>
+ <reference key="IBUIFontDescription" ref="1026807309"/>
+ <reference key="IBUIFont" ref="556124141"/>
+ </object>
</object>
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
<reference key="NSSuperview"/>
@@ -322,21 +348,21 @@ AAgACAAIAAEAAQABAAE</bytes>
<int key="connectionID">9</int>
</object>
<object class="IBConnectionRecord">
- <object class="IBCocoaTouchOutletConnection" key="connection">
- <string key="label">delegate</string>
- <reference key="source" ref="117506181"/>
+ <object class="IBCocoaTouchEventConnection" key="connection">
+ <string key="label">selectAnAnimal:</string>
+ <reference key="source" ref="506316691"/>
<reference key="destination" ref="372490531"/>
+ <int key="IBEventType">1</int>
</object>
- <int key="connectionID">16</int>
+ <int key="connectionID">34</int>
</object>
<object class="IBConnectionRecord">
- <object class="IBCocoaTouchEventConnection" key="connection">
- <string key="label">selectAnItem:</string>
- <reference key="source" ref="506316691"/>
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">delegate</string>
+ <reference key="source" ref="117506181"/>
<reference key="destination" ref="372490531"/>
- <int key="IBEventType">1</int>
</object>
- <int key="connectionID">23</int>
+ <int key="connectionID">16</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
@@ -364,6 +390,23 @@ AAgACAAIAAEAAQABAAE</bytes>
</object>
<int key="connectionID">29</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">delegate</string>
+ <reference key="source" ref="424092331"/>
+ <reference key="destination" ref="372490531"/>
+ </object>
+ <int key="connectionID">31</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchEventConnection" key="connection">
+ <string key="label">selectABlock:</string>
+ <reference key="source" ref="424092331"/>
+ <reference key="destination" ref="372490531"/>
+ <int key="IBEventType">1</int>
+ </object>
+ <int key="connectionID">33</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -396,9 +439,10 @@ AAgACAAIAAEAAQABAAE</bytes>
<reference ref="685455592"/>
<reference ref="1004339132"/>
<reference ref="502593390"/>
- <reference ref="117506181"/>
<reference ref="506316691"/>
<reference ref="366061364"/>
+ <reference ref="117506181"/>
+ <reference ref="424092331"/>
</object>
<reference key="parent" ref="0"/>
</object>
@@ -437,6 +481,11 @@ AAgACAAIAAEAAQABAAE</bytes>
<reference key="object" ref="366061364"/>
<reference key="parent" ref="774585933"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">30</int>
+ <reference key="object" ref="424092331"/>
+ <reference key="parent" ref="774585933"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -453,6 +502,7 @@ AAgACAAIAAEAAQABAAE</bytes>
<string>15.IBPluginDependency</string>
<string>18.IBPluginDependency</string>
<string>25.IBPluginDependency</string>
+ <string>30.IBPluginDependency</string>
<string>6.IBPluginDependency</string>
<string>8.IBPluginDependency</string>
</object>
@@ -470,6 +520,7 @@ AAgACAAIAAEAAQABAAE</bytes>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
+ <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
@@ -484,7 +535,7 @@ AAgACAAIAAEAAQABAAE</bytes>
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
- <int key="maxID">29</int>
+ <int key="maxID">34</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -496,12 +547,18 @@ AAgACAAIAAEAAQABAAE</bytes>
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
+ <string>animalButtonTapped:</string>
+ <string>dateButtonTapped:</string>
+ <string>selectABlock:</string>
<string>selectADate:</string>
<string>selectAMeasurement:</string>
- <string>selectAnItem:</string>
+ <string>selectAnAnimal:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
+ <string>UIBarButtonItem</string>
+ <string>UIBarButtonItem</string>
+ <string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
@@ -511,13 +568,28 @@ AAgACAAIAAEAAQABAAE</bytes>
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
+ <string>animalButtonTapped:</string>
+ <string>dateButtonTapped:</string>
+ <string>selectABlock:</string>
<string>selectADate:</string>
<string>selectAMeasurement:</string>
- <string>selectAnItem:</string>
+ <string>selectAnAnimal:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBActionInfo">
+ <string key="name">animalButtonTapped:</string>
+ <string key="candidateClassName">UIBarButtonItem</string>
+ </object>
+ <object class="IBActionInfo">
+ <string key="name">dateButtonTapped:</string>
+ <string key="candidateClassName">UIBarButtonItem</string>
+ </object>
+ <object class="IBActionInfo">
+ <string key="name">selectABlock:</string>
+ <string key="candidateClassName">id</string>
+ </object>
+ <object class="IBActionInfo">
<string key="name">selectADate:</string>
<string key="candidateClassName">id</string>
</object>
@@ -526,7 +598,7 @@ AAgACAAIAAEAAQABAAE</bytes>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
- <string key="name">selectAnItem:</string>
+ <string key="name">selectAnAnimal:</string>
<string key="candidateClassName">id</string>
</object>
</object>
@@ -550,6 +622,6 @@ AAgACAAIAAEAAQABAAE</bytes>
</object>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
- <string key="IBCocoaTouchPluginVersion">884</string>
+ <string key="IBCocoaTouchPluginVersion">933</string>
</data>
</archive>
View
18 Sample/Classes/ActionSheetPickerViewController.h
@@ -28,28 +28,20 @@
#import <UIKit/UIKit.h>
-#import "ActionSheetPicker.h"
-
-@interface ActionSheetPickerViewController : UIViewController <UITextFieldDelegate, ActionSheetPickerDelegate>
-
+@class AbstractActionSheetPicker;
+@interface ActionSheetPickerViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic, retain) NSArray *animals;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, retain) NSDate *selectedDate;
@property (nonatomic, assign) NSInteger selectedBigUnit;
@property (nonatomic, assign) NSInteger selectedSmallUnit;
-@property (nonatomic, retain) ActionSheetPicker *actionSheetPicker;
+@property (nonatomic, retain) AbstractActionSheetPicker *actionSheetPicker;
-- (IBAction)selectAnItem:(id)sender;
+- (IBAction)selectABlock:(id)sender;
+- (IBAction)selectAnAnimal:(id)sender;
- (IBAction)selectADate:(id)sender;
- (IBAction)animalButtonTapped:(UIBarButtonItem *)sender;
- (IBAction)dateButtonTapped:(UIBarButtonItem *)sender;
- (IBAction)selectAMeasurement:(id)sender;
-- (void)measurementWasSelectedWithBigUnit:(NSNumber *)bigUnit smallUnit:(NSNumber *)smallUnit element:(id)element;
-- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element;
-- (void)itemWasSelected:(NSNumber *)selectedIndex element:(id)element;
-
-- (void)actionPickerCancelled;
-- (void)actionPickerDoneWithValue:(id)value;
-
@end
View
138 Sample/Classes/ActionSheetPickerViewController.m
@@ -27,14 +27,18 @@
#import "ActionSheetPickerViewController.h"
-#import "ActionSheetDistancePicker.h"
-#import "ActionSheetDatePicker.h"
+#import "ActionSheetPicker.h"
#import "NSDate+TCUtils.h"
+@interface ActionSheetPickerViewController()
+- (void)measurementWasSelectedWithBigUnit:(NSNumber *)bigUnit smallUnit:(NSNumber *)smallUnit element:(id)element;
+- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element;
+- (void)animalWasSelected:(NSNumber *)selectedIndex element:(id)element;
+@end
+
@implementation ActionSheetPickerViewController
@synthesize animals = _animals;
-
@synthesize selectedIndex = _selectedIndex;
@synthesize selectedDate = _selectedDate;
@synthesize selectedBigUnit = _selectedBigUnit;
@@ -43,71 +47,64 @@ @implementation ActionSheetPickerViewController
- (void)viewDidLoad {
[super viewDidLoad];
-
self.animals = [NSArray arrayWithObjects:@"Aardvark", @"Beaver", @"Cheetah", @"Deer", @"Elephant", @"Frog", @"Gopher", @"Horse", @"Impala", @"...", @"Zebra", nil];
self.selectedDate = [NSDate date];
}
+- (void)dealloc {
+ self.animals = nil;
+ self.selectedDate = nil;
+ self.actionSheetPicker = nil;
+ [super dealloc];
+}
+
- (void)viewDidUnload {
self.actionSheetPicker = nil;
[super viewDidUnload];
}
-#pragma mark -
-#pragma mark IBActions
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+{
+ return YES;
+}
-- (IBAction)selectAnItem:(UIControl *)sender {
- [ActionSheetPicker showPickerWithTitle:@"Select Animal"
- rows:self.animals
- initialSelection:self.selectedIndex
- target:self
- action:@selector(itemWasSelected:element:)
- origin:sender];
-
-
- //Example ActionSheetPicker using customButtons
-// self.actionSheetPicker = [[ActionSheetPicker alloc] initWithTitle:@"Select Animal"
-// rows:self.animals
-// initialSelection:self.selectedIndex
-// target:self
-// action:@selector(itemWasSelected:element:)
-// origin:sender];
-//
-// [self.actionSheetPicker addCustomButtonWithTitle:@"Special" value:[NSNumber numberWithInt:1]];
-// self.actionSheetPicker.hideCancel = YES;
-//
-// [self.actionSheetPicker showActionSheetPicker];
+#pragma mark - IBActions
+
+- (IBAction)selectABlock:(UIControl *)sender {
+ ActionStringDoneBlock done = ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
+ if ([sender respondsToSelector:@selector(setText:)]) {
+ [sender performSelector:@selector(setText:) withObject:selectedValue];
+ }
+ };
+ ActionStringCancelBlock cancel = ^(ActionSheetStringPicker *picker) {
+ NSLog(@"Block Picker Canceled");
+ };
+ NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil];
+ [ActionSheetStringPicker showPickerWithTitle:@"Select a Block" rows:colors initialSelection:0 doneBlock:done cancelBlock:cancel origin:sender];
+}
+
+- (IBAction)selectAnAnimal:(UIControl *)sender {
+ [ActionSheetStringPicker showPickerWithTitle:@"Select Animal" rows:self.animals initialSelection:self.selectedIndex target:self sucessAction:@selector(animalWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
+ /* Example ActionSheetPicker using customButtons
+ self.actionSheetPicker = [[ActionSheetPicker alloc] initWithTitle@"Select Animal" rows:self.animals initialSelection:self.selectedIndex target:self sucessAction:@selector(itemWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender
+
+ [self.actionSheetPicker addCustomButtonWithTitle:@"Special" value:[NSNumber numberWithInt:1]];
+ self.actionSheetPicker.hideCancel = YES;
+ [self.actionSheetPicker showActionSheetPicker];
+ */
}
- (IBAction)selectADate:(UIControl *)sender {
-
- //this example uses custom buttons and demonstrates delegate protocol
-
- _actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@""
- datePickerMode:UIDatePickerModeDate
- selectedDate:self.selectedDate
- target:self
- action:@selector(dateWasSelected:element:)
- origin:sender];
-
+ _actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:self.selectedDate target:self action:@selector(dateWasSelected:element:) origin:sender];
[self.actionSheetPicker addCustomButtonWithTitle:@"Today" value:[NSDate date]];
- [self.actionSheetPicker addCustomButtonWithTitle:@"Yesterday" value:[[NSDate date] dateByAdddingCalendarUnits:NSDayCalendarUnit amount:-1]];
+ [self.actionSheetPicker addCustomButtonWithTitle:@"Yesterday" value:[[NSDate date] TC_dateByAddingCalendarUnits:NSDayCalendarUnit amount:-1]];
self.actionSheetPicker.hideCancel = YES;
- self.actionSheetPicker.delegate = self;
-
[self.actionSheetPicker showActionSheetPicker];
-
-// [ActionSheetDatePicker showPickerWithTitle:@"Select Date"
-// datePickerMode:UIDatePickerModeDate
-// selectedDate:self.selectedDate
-// target:self
-// action:@selector(dateWasSelected:element:)
-// origin:sender];
}
- (IBAction)animalButtonTapped:(UIBarButtonItem *)sender {
- [self selectAnItem:sender];
+ [self selectAnAnimal:sender];
}
- (IBAction)dateButtonTapped:(UIBarButtonItem *)sender {
@@ -115,25 +112,18 @@ - (IBAction)dateButtonTapped:(UIBarButtonItem *)sender {
}
- (IBAction)selectAMeasurement:(UIControl *)sender {
- [ActionSheetDistancePicker showPickerWithTitle:@"Select Length"
- bigUnitString:@"m" bigUnitMax:330 selectedBigUnit:self.selectedBigUnit
- smallUnitString:@"cm" smallUnitMax:99 selectedSmallUnit:self.selectedSmallUnit
- target:self
- action:@selector(measurementWasSelectedWithBigUnit:smallUnit:element:)
- origin:sender];
+ [ActionSheetDistancePicker showPickerWithTitle:@"Select Length" bigUnitString:@"m" bigUnitMax:330 selectedBigUnit:self.selectedBigUnit smallUnitString:@"cm" smallUnitMax:99 selectedSmallUnit:self.selectedSmallUnit target:self action:@selector(measurementWasSelectedWithBigUnit:smallUnit:element:) origin:sender];
}
-#pragma mark -
-#pragma mark Implementation
+#pragma mark - Implementation
-- (void)itemWasSelected:(NSNumber *)selectedIndex element:(id)element {
+- (void)animalWasSelected:(NSNumber *)selectedIndex element:(id)element {
self.selectedIndex = [selectedIndex intValue];
if ([element respondsToSelector:@selector(setText:)]) {
[element setText:[self.animals objectAtIndex:self.selectedIndex]];
}
}
-
- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element {
self.selectedDate = selectedDate;
if ([element respondsToSelector:@selector(setText:)]) {
@@ -147,37 +137,15 @@ - (void)measurementWasSelectedWithBigUnit:(NSNumber *)bigUnit smallUnit:(NSNumbe
[element setText:[NSString stringWithFormat:@"%i m and %i cm", [bigUnit intValue], [smallUnit intValue]]];
}
-#pragma mark -
-#pragma mark UITextFieldDelegate
-
-- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
- return NO;
-}
-
-#pragma mark -
-#pragma mark Memory Management
-
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
-{
- return YES;
-}
-
-
-- (void)dealloc {
- self.animals = nil;
- self.selectedDate = nil;
- self.actionSheetPicker = nil;
- [super dealloc];
+- (void)actionPickerCancelled:(id)sender {
+ NSLog(@"Delegate has been informed that ActionSheetPicker was cancelled");
}
-#pragma - ActionSheetPickerDelegate
+#pragma mark - UITextFieldDelegate
-- (void)actionPickerCancelled {
- NSLog(@"Delegate has been informed that ActionSheetPicker was cancelled");
+- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
+ return NO;
}
-- (void)actionPickerDoneWithValue:(id)value {
- NSLog(@"Delegate has been informed that ActionSheetPicker completed with value: %@", value);
-}
@end
View
6 Sample/Classes/NSDate+TCUtils.h
@@ -8,8 +8,6 @@
#import <Foundation/Foundation.h>
-@interface NSDate (TCUtils)
-
-- (NSDate *)dateByAdddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount;
-
+@interface NSDate(TCUtils)
+- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount;
@end
View
2 Sample/Classes/NSDate+TCUtils.m
@@ -10,7 +10,7 @@
@implementation NSDate (TCUtils)
-- (NSDate *)dateByAdddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount {
+- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount {
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *newDate;

0 comments on commit 222f908

Please sign in to comment.
Something went wrong with that request. Please try again.