Giter VIP home page Giter VIP logo

actionsheetpicker's People

Contributors

0xmark avatar brettg avatar carlostse avatar delackner avatar ecordell avatar eloe avatar elwerene avatar gituser03 avatar greenwoodcm avatar grgcombs avatar ignazioc avatar jaseelder avatar jkrzemie avatar johnnyg avatar jparise avatar markrickert avatar mau04 avatar nimuzak avatar ronakjangir47 avatar skywinder avatar stupergenius avatar timcinel avatar typeoneerror avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

actionsheetpicker's Issues

Problem with popover positioning

Hi Tim

I would really want to use your ActionSheetPicker. I encountered 2 problems with iPad popover positioning.

  1. I dynamically add a UIButton in a viewDidLoad and the picker must appear when clicking on the button. It works well on iPhone but on iPad the popover is not well positioned. When rewriting presentPickerForView as below it worked :
  • (void)presentPickerForView:(UIView *)aView {

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    self.presentFromRect = _containerView.frame;
    [self configureAndPresentPopoverForView:aView];
    }
    else {
    self.presentFromRect = aView.frame;
    [self configureAndPresentActionSheetForView:aView];
    }
    }

  1. I did the same in a project looking very similar to your sample and the popovers are not well positioned. If I use the built-in version it works for 2 over 3 of the textFields...

I am very puzzled by this instruction "self.presentFromRect = aView.frame;" in case of popovers.
I do not understand why the presentFromRect is not the rectangle associated to where the popover is displayed and for me aView.frame is the frame of the picked itself with x and y equal to 0, width 320 end height:260.

I would greatly appreciate your help.
Many thanks in advance
Dominique

Memory leaks?

The analyze tool is complaining about how actionSheetPicker doesn't get released.

  • (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
    ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action title:title];
    actionSheetPicker.convenientObject = YES;
    [actionSheetPicker showActionPicker];
    }
  • (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title{
    ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title];
    actionSheetPicker.convenientObject = YES;
    [actionSheetPicker showActionPicker];
    }

Orientation Issue

PopOverController gets displaced when the orientation is changed.Is there a way to either retain its position or dismiss it when the orientation gets changed ? I've been trying hard to dismiss the popOvercontroller when the orientation changes but not able to do it.Please help me with the solution.

dismiss when tap outside the actionsheet

I fork a project from you and add this function which is very useful. Hope you can add this into master branch.

Simply add a bool property dismissWhenTapOut, and add the following code in the end of "void showActionSheetPicker":

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
    if(self.dismissWhenTapOut){
        tap.cancelsTouchesInView = NO;
        [self.actionSheet.superview addGestureRecognizer:tap];
    }
    [tap release];

Finally, add a function inside the same file:

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.actionSheet];
    if (p.y < 0) {
        [self dismissPicker];
    }
}

My branch is here: https://github.com/benck/ActionSheetPicker

Finicky buttons on ActionSheetDatePicker

When adding an ActionSheetDatePicker to a UITableViewController the buttons only accept user presses in the bottom right corner.

Pressing the "done" button in the top left corner for example is not registered as a press.

The same button works like it should when the device is rotated to landscape.

NOTE: You can see this defect in action in the sample application. Just run the sample select the date field and try to press the done button in the upper left corner.

Popover doesn't show up on ipad

With the new build, ipad support is mentioned. But on my implementation, I only see the tiniest bit of the popover border on the ipad, down at the bottom of the screen, not the expected popover view. My view is inside a tabbarcontroller, don't know if that would have something to do with it.

Seems like UItextfield is overridden

After implementing your package (which is splendid), I cannot implement IBAction such as "Editing Did End" and "Value Changed".

How can I detect a change of value for a UItextfield which implements your ActionSheetPicker? Or how can I detect when I finish editing for a UItextfield which implements your ActionSheetPicker?

I can send u the code if u need. Thank you so much for your help!

Please add semantic version tags

I’ve recently added ActionSheetPicker to the CocoaPods package manager repo.

CocoaPods is a tool for managing dependencies for OSX and iOS Xcode projects and provides a central repository for iOS/OSX libraries. This makes adding libraries to a project and updating them extremely easy and it will help users to resolve dependencies of the libraries they use.

However, ActionSheetPicker doesn't have any version tags. I’ve added the current HEAD as version 0.0.1, but a version tag will make dependency resolution much easier.

Semantic version tags (instead of plain commit hashes/revisions) allow for resolution of cross-dependencies.

In case you didn’t know this yet; you can tag the current HEAD as, for instance, version 1.0.0, like so:

$ git tag -a 1.0.0 -m "Tag release 1.0.0"
$ git push --tags

UIPopoverController position

If you move an UITextField higher in xib, so UIPopoverController displays in UIPopoverArrowDirectionUp mode, there is a ~250px space between the popover and the textfield. Tested in simulator, iOS 5.

Suggestion: enable to hook customButtonPressed on the target view

Hello, I needed to perform some custom actions when touching a specific custom button.

So I made a change to enable the customButtonPressed: to be hooked in the target view.

By doing this I can perform what I need in my view and then continue the default behaviors by calling [self.actionSheetPicker customButtonPressed:sender];

As I'm new to GitHub collaboration, I'll paste the code below here:

AbstractActionSheetPicker.m : 209
if([self.target respondsToSelector:@selector(customButtonPressed:)])
{
button = [[UIBarButtonItem alloc] initWithTitle:buttonTitle style:UIBarButtonItemStyleBordered target:self.target action:@selector(customButtonPressed:)];
}
else
{
button = [[UIBarButtonItem alloc] initWithTitle:buttonTitle style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonPressed:)];
}

iOS 6 - createToolbarLabelWithTitle - UITextAlignmentCenter

Hi Tim, I found something minor for iOS 6 compatibility. In AbstractActionSheetPicker.m, the method createToolbarLabelWithTitle is leveraging UITextAlignmentCenter, and it's deprecated at 2.0. Recommendations to use NSTextAlignmentCenter. Probably thought of as a minor issue.
By the way, I'm liking ActionSheet Picker a lot! I was trying to use it with a static table view with strange results but then went with a UIViewController instead. Cheers!

Feature Request

Hi, I came across this project today and it fits perfectly with what I'm wanting to do. So, big thanks for sharing this with the dev world!!

However, being that I'm a noob to iOS development, I'm having great difficulty in trying to figure out how to make this work with a multi-column numeric data format. For example I would like the data source to be 0 to 50 Kgs with grams, or four numeric columns (1st = 0-50 KG, 2nd = 0-9 (hundreds of) grams, 3rd = 0-9 (tens of) grams, 4th = 0-9 grams.

Any insight/modifications in order to make it work with this type of data?

Custom Objects

I do some modifications, and now we able to pass an array of custom objects with a new string argument with the name of the property we want to show in the picker, instead of just array of strings.

How can I share the code??

ActionSheetPicker width on iPad during a landscape orientation

First I would like to say that this ActionSheetPicker saved me a LOT of time in creating a custom picker. Thank you so much Tim and the other devs who have touched this. So i thought I might do my part in contributing to the betterment of this custom control.

I'm currently using this in a universal app I'm working on and I've noticed that when you show the ActionSheetPicker on the iPad with a landscape orientation, the ActionSheetPicker is stretched. Now I dug a little in the source code and here's what I've updated in the file AbstractActionSheetPicker.m:

- (CGSize)viewSize {
  if (![self isViewPortrait]) {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
      return CGSizeMake(480, 320);
    } else {
      return CGSizeMake(320, 480);
    }
  }
    return CGSizeMake(320, 480);
}

Feature Request: ipad support

First of all, great class. Exactly what I was looking for. Thank you!
I was trying in a ipad project I have been working on and having some issues. The UIActionSheet comes up but size is messed up(comes up with just a thin dark line). I have tried replacing some of the frame values but cannot get working.

Interface Orientation Support

Hello There
thank you for this peace of code, it helped me a lot.
I have just a problem to make it support Interface orientation : When the device is in landscape mode the UIPickerView is deformed..
If you have any suggestion i will be very pleased ..
I'll let you know if i have a solution
Thanks

Closing picker during inertial scrolling causes crash

Hello, and thanks for this great utility. I believe I have found a bug: if you have a big enough list (10+ items) and flick the list so that it performs inertial scrolling, clicking "done"' or "cancel" before the scrolling has completed causes the program to crash with an EXEC_BAD_ACCESS error.

I am able to duplicate this in the simulator and on my iPhone 4S. I tried finding the issue, but I'm not great at debugging. It seems to be happening after the picker has already been dismissed and deallocated. Please let me know if there is any more information I can provide to help resolve this.

Button area slightly off

very first button is good but right portion of second button and big chunk of portion of cancel button is not respondng well.

is this a known issue?

thanks in advance!

the following is all i am doing btw.

    ActionSheetDatePicker *_actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@""
                                                                              datePickerMode:UIDatePickerModeDate
                                                                                selectedDate:[NSDate date]
                                                                                      target:self
                                                                                      action:@selector(dateWasSelected:element:)
                                                                                      origin:self.view];
    [_actionSheetPicker addCustomButtonWithTitle:@"Today" value:[NSDate date]];
    _actionSheetPicker.hideCancel = NO;
    [_actionSheetPicker showActionSheetPicker];

target leak?

Hi, don't you need to release the 'target' property in dealloc?

momery leak

when you set selfReference
self.selfReference = self;
release method will never calls

16 pull requests and 42 issues

Hi Tim: Have you recently considered addressing the 16 pull requests and/or 42 open issues? I like using your project in several projects. -Richard

release problem

when tap anywhere to dismiss the ActionSheetPicker (without click cancel and done button)
the ActionSheetPicker won't release

displayActionPickerWithView not work on 3.2 Simulator

I found that the method displayActionPickerWithView not work with the 3.2 Simulator. The action sheet is shown there and it has no response no matter where I touch on the screen. The problem seems to be resolved if I update the line to show the action sheet like this:

[self.actionSheet showInView:[self.view window]];

instead of the original line

[self.actionSheet showInView:self.view];

Feature Requests: API extension for better integration

  1. ActionPickerSheet should get a delegate target with protocol. e.g. the actionPickerCancel method should inform the delegate of the cancel to do cleanup within the calling (typically) UIViewController
  2. Extend the initializer to deliver back the created ActionPickerSheet (refer to issue #13 "Orientation issue" for details

UIActionSheet not closing on iPhone 4.0, 4.1 simulator

Hi Tim,

Thanks a lot for this utility. Just included it in my app and works well, expect for one issue.

In the iPhone 4.0, 4.1 simulator the UIActionSheet is visible and the UIPickerView's data is lost when the cancel or done button is pressed. Not sure if the same behavior is present when running on the device. Please let me know if you require more information to reproduce this.

Thanks.

Action Sheet Done Button Color

image

For some reason, the text color of my Done and Cancel buttons is black. Where can I change this? I've looked a little in the code, but I'm wondering if anyone here has experienced something similar.

I'd appreciate any help, thanks!

Suggestion: Use a prefix

Hi,

Sorry if this is not the proper way of giving you suggestions, I was trying to find a comment section but didn't.
This is not actually an issue however I would suggest the use of a prefix. Your classes should start with TC.
For example, ActionSheetDatePicker.h should be renamed TCActionSheetDatePicker.h.

This would ensure that there is no conflict with classes from other project. A bit like a package identifier (com.domain.package.classes) in Java.

Thanks for reading.
Ben

ios 3.1.3 error

Hi was getting this error on IOS 3.1.3 dyld: symbol not found: _Block_copy referenced from : var /mobile/applications/....../my.appp/app
Expected in: /usr/loib/libSystem.B.dylib

any ideas on how to resolve this?

UIPopoverController and UITableViewCell

In order to place UIPopoverController on Table Cell, I've done this small quick fix, maybe it can help someone else. It works in my case, not sure if it will work in any case ;).

in :

- (void)presentPopover:(UIPopoverController *)popover

just put

if (self.barButtonItem) {
        [popover presentPopoverFromBarButtonItem:_barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        return;
    }
    else if ((self.containerView) && NO == CGRectIsEmpty(self.presentFromRect)) {
        NSParameterAssert(popover != NULL);
        if ([self.containerView isKindOfClass:[UITableViewCell class]]) {
            UITableViewCell *cell = (UITableViewCell*) self.containerView;
            UIButton *button = (UIButton *)cell.accessoryView;
            _presentFromRect = cell.bounds;
            _presentFromRect.origin.x = button.bounds.size.width / 2;
            [popover presentPopoverFromRect:_presentFromRect inView:_containerView permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
            return;
        } else {
            [popover presentPopoverFromRect:_presentFromRect inView:_containerView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            return;
        }
    }

I'm new to Objective-c, so this peace of code can probably be enhanced.

Nicolas

Keyboard displayed when ActionSheetPicker pops up

I've integrated the ARC branch provided here into my iPad project. Basically, everything works fine. However, as soon as the ActionSheetPicker pops up the keyboard will be shown.

Does anyone have an idea what I forgot here?

Thanks

ActionSheet Picker wrong size if about to remove keyboard using resign First Responder

Using an iPad/iPad Simulator.

I've got a textfield and I've overridden its behaviour using textFieldShouldBeginEditing for that specific field so that it immediately resigns the first responder and displays the picker instead. (Its displaying a range of ages)

If its the first field then everything is OK and the picker is displayed correctly - updating the textfield once Done is selected.

However if I'm editing another text field (changing its data such as a name field ) and then I select the overridden textField
the picker comes out the wrong size and flips above the field instead of being below it as before.

I've tried to work out where in the code it sets the size but even setting it manually doesn't work.

Suggestions?

DistancePickerView is required (Refactor?)

So the DistancePickerView is required when compiling the ActionSheetPicker into another project. Yet, the DistancePickerView is found in the demo project directory, not in the main directory with ActionSheetPicker. In the interest of reducing unnecessary dependencies, what about factoring out the various ActionSheetPicker variants into their own class or at least into separate categories? This way, the core picker code is largely unchanged in the event of new picker additions.

Landscape Picker Cut Off

Hello,

Thanks for the great library. While working with it I noticed that opening the picker while in landscape mode on an iPhone causes the picker basically to display as though it is cut off. It looks like the action sheet doesn't come far enough up in the view to show the whole picker. I have only tested with iOS 5. The picker for the iPad still works with iOS 5 and landscape mode.This is replicable in the samples, so it is consistent.

If I have time I'll take a stab at fixing it.

Thanks,
Ben.

Picker view goes blank during dismiss animation

Would look nicer if it didn't go white and the content was still there when it dismissed, like the regular UIPickerController.

self.selfReference = nil; in dismissPicker causes the deallocation to early.

Origin UIView should resize with animation

When the picker view is shown, it would be stupendous if the origin UIView could be made to automagically resize in tandem with the picker's appearance/disappearance. For instance, UITableView does this when the keyboard is displayed, and helps maintain visual context. This may be an optional setting. Thoughts?

uitableview multiple uitextfields

Do you have an example on how to use this with uitableview textfield cells?

I've put 3 text fields in a uitableview. Each row has one tableview.
each field has different array of values (text) to put in.

I don't really know how to solve this.
Can anyone help me?

Thanks.

License?

It would probably be a good idea to actually give this a proper license (e.g. GPL, BSD, etc.) so that people can use it in commercial apps without having to worry about licensing issues.

Suggestion on approach - 2 components

I'm having difficulty figuring out which of the UIPicker classes to leverage for a dual component picker where the first component's selection will drive the second components returned values (array). I can do this in native iOS means, but I'm not sure if I should extend the ActionSheetStringPicker, ActionSheetPickerCustomPickerDeligate, etc.
(based on the sample project). This is a good problem, but I'm currently a bit confused.

For example, I'm using a construct like this to switch the second component:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component ==0)
{
return [Countries objectAtIndex:row];
}
else {
if ([club isEqualToString:@"United States"]) {
return [usArray objectAtIndex:row];
}
if ([club isEqualToString:@"Germany"]) {
return [germanyArray objectAtIndex:row];
}
//if....
if ([club isEqualToString:@"England"]) {
return [englandArray objectAtIndex:row];
}
}

return 0;

}

Feature: Arbitrary Value Buttons

Offer the option to spawn an ActionSheetPicker with buttons mapped to values.

ie.
Item picker mode: assign buttons to array indexes.
Date picker mode: assign buttons to dates.

The button captions and values would be passed in as separate NSArrays, most likely, as NSDictionarys would not allow choice of order.

Support multiple components with different size, and support minimum date for datepicker

Terribly sorry, I don't know how to use github, pretty new here. I really like your library, and made a few improvement to support multiple components, size, and also support minimum date.

//
// ActionSheetPicker.h
// Spent
//
// Created by Tim Cinel on 3/01/11.
// Copyright 2011 Thunderous Playground. All rights reserved.
//
//
// Easily present an ActionSheet with a PickerView to select from a number of immutible options,
// based on the drop-down replacement in mobilesafari.
//
// Some code derived from marcio's post on Stack Overflow [ http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how ]

import <Foundation/Foundation.h>

@interface ActionSheetPicker : NSObject <UIPickerViewDelegate, UIPickerViewDataSource> {
UIView *_view;

NSArray *_data;
NSInteger _selectedIndex;

NSArray *_multipleComponentsData;
NSMutableArray *_multipleComponentsSelectedIndex;
NSArray *_componentWidths;
NSString *_title;

UIDatePickerMode _datePickerMode;
NSDate *_selectedDate;
NSDate *_minimumDate;

id _target;
SEL _action;

UIActionSheet *_actionSheet;
UIPickerView *_pickerView;
UIDatePicker *_datePickerView;
NSInteger _pickerPosition;

BOOL _convenientObject;

}

@Property (nonatomic, retain) UIView *view;

@Property (nonatomic, retain) NSArray *data;
@Property (nonatomic, assign) NSInteger selectedIndex;

@Property (nonatomic, retain) NSArray *multipleComponentsData;
@Property (nonatomic, retain) NSMutableArray *multipleComponentsSeletedIndex;
@Property (nonatomic, retain) NSArray *componentWidths;
@Property (nonatomic, copy) NSString *title;

@Property (nonatomic, assign) UIDatePickerMode datePickerMode;
@Property (nonatomic, retain) NSDate *selectedDate;
@Property (nonatomic, retain) NSDate *minimumDate;

@Property (nonatomic, retain) id target;
@Property (nonatomic, assign) SEL action;

@Property (nonatomic, retain) UIActionSheet *actionSheet;
@Property (nonatomic, retain) UIPickerView *pickerView;
@Property (nonatomic, retain) UIDatePicker *datePickerView;
@Property (nonatomic, assign) NSInteger pickerPosition;
@Property (nonatomic, assign) BOOL convenientObject;

//no memory management required for convenience methods

//display actionsheet picker inside View, loaded with strings from data, with item selectedIndex selected. On dismissal, [target action:(NSNumber *)selectedIndex] is called
//+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title;

//display actionsheet datepicker in datePickerMode inside View with selectedDate selected. On dismissal, [target action:(NSDate *)selectedDate] is called
//+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;

  • (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action;
  • (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)someData selectedIndex:(NSInteger)index target:(id)target action:(SEL)action title:(NSString *)title;
  • (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title;
  • (id)initForDateWithContainingView:(UIView )aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate)minimumDate target:(id)target action:(SEL)action title:(NSString *)title;
  • (id)initForDataWithContainingView:(UIView )aView
    multipleComponentsData:(NSArray *)someData
    multipleComponentsSelectedIndex:(NSMutableArray
    )indexArray
    componentWidths:(NSArray*)componentWidths
    target:(id)target
    action:(SEL)action
    title:(NSString *)title;

//implementation

  • (void)showActionPicker;
  • (void)showDataPicker;
  • (void)showDatePicker;
  • (void)actionPickerDone;
  • (void)actionPickerCancel;
  • (void)eventForDatePicker:(id)sender;
  • (BOOL)isViewPortrait;

@EnD

//
// ActionSheetPicker.m
// Spent
//
// Created by Tim Cinel on 3/01/11.
// Copyright 2011 Thunderous Playground. All rights reserved.
//

import "ActionSheetPicker.h"

@implementation ActionSheetPicker

@synthesize view = _view;

@synthesize data = _data;
@synthesize selectedIndex = _selectedIndex;

@synthesize multipleComponentsData = _multipleComponentsData;
@synthesize multipleComponentsSeletedIndex = _multipleComponentsSelectedIndex;
@synthesize componentWidths = _componentWidths;
@synthesize title = _title;

@synthesize selectedDate = _selectedDate;
@synthesize datePickerMode = _datePickerMode;
@synthesize minimumDate = _minimumDate;

@synthesize target = _target;
@synthesize action = _action;

@synthesize actionSheet = _actionSheet;
@synthesize pickerView = _pickerView;
@synthesize datePickerView = _datePickerView;
@synthesize pickerPosition = _pickerPosition;

@synthesize convenientObject = _convenientObject;

pragma mark -

pragma mark NSObject

//+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
// ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action title:title];
// actionSheetPicker.convenientObject = YES;
// [actionSheetPicker showActionPicker];
//}
//
//+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title{
// ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title];
// actionSheetPicker.convenientObject = YES;
// [actionSheetPicker showActionPicker];
//}

  • (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action {
    if ((self = [super init]) != nil) {
    self.view = aView;
    self.target = target;
    self.action = action;
    }
    return self;
    }
  • (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title{
    if ([self initWithContainingView:aView target:target action:action] != nil) {
    self.data = data;
    self.selectedIndex = selectedIndex;
    self.title = title;
    }
    return self;
    }
  • (id)initForDataWithContainingView:(UIView )aView
    multipleComponentsData:(NSArray *)someData
    multipleComponentsSelectedIndex:(NSMutableArray
    )indexArray
    componentWidths:(NSArray*)componentWidths
    target:(id)target
    action:(SEL)action
    title:(NSString *)title {
    if ([self initWithContainingView:aView target:target action:action] != nil) {
    self.multipleComponentsData = someData;
    self.multipleComponentsSeletedIndex = indexArray;
    self.title = title;
    self.componentWidths = componentWidths;
    }
    return self;
    }
  • (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title {
    if ([self initWithContainingView:aView target:target action:action] != nil) {
    self.datePickerMode = datePickerMode;
    self.selectedDate = selectedDate;
    self.title = title;
    }
    return self;
    }
  • (id)initForDateWithContainingView:(UIView )aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate minimumDate:(NSDate)minimumDate target:(id)target action:(SEL)action title:(NSString *)title {
    if ([self initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title] != nil) {
    self.minimumDate = minimumDate;
    };
    return self;
    }

pragma mark -

pragma mark Implementation

  • (void)showActionPicker {
    //spawn actionsheet
    _actionSheet = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?nil:@"\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    if (nil != self.data || nil != self.multipleComponentsData)
    //show data picker
    [self showDataPicker];
    else
    //show date picker
    [self showDatePicker];

    UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 480, 44)];
    pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel)];
    [barItems addObject:cancelBtn];
    [cancelBtn release];

    UIBarButtonItem *flexSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
    [barItems addObject:flexSpace];

    //Add tool bar title label
    if (nil != self.title){
    UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];

    [toolBarItemlabel setTextAlignment:UITextAlignmentCenter];  
    [toolBarItemlabel setTextColor:[UIColor whiteColor]];   
    [toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];    
    [toolBarItemlabel setBackgroundColor:[UIColor clearColor]]; 
    toolBarItemlabel.text = self.title; 
    
    UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
    [toolBarItemlabel release]; 
    [barItems addObject:buttonLabel];   
    [buttonLabel release];  
    
    [barItems addObject:flexSpace];
    

    }

    //add "Done" button
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone)];
    [barItems addObject:barButton];
    [barButton release];

    [pickerDateToolbar setItems:barItems animated:YES];
    [barItems release];

    [self.actionSheet addSubview:pickerDateToolbar];
    [pickerDateToolbar release];

    [self.actionSheet showInView:self.view];
    if ( [self isViewPortrait] )
    [self.actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    else
    [self.actionSheet setBounds:CGRectMake(0, 0, 480, 325)];
    }

  • (void)showDataPicker {
    //spawn pickerview
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    _pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];

    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.showsSelectionIndicator = YES;
    if (self.multipleComponentsSeletedIndex != nil) {
    for (int component=0; component<[self.multipleComponentsSeletedIndex count]; component++) {
    NSNumber *number = [self.multipleComponentsSeletedIndex objectAtIndex:component];
    [self.pickerView selectRow:[number intValue] inComponent:component animated:NO];
    }
    }
    else {
    [self.pickerView selectRow:self.selectedIndex inComponent:0 animated:NO];
    }

    [self.actionSheet addSubview:self.pickerView];
    }

  • (void)showDatePicker {
    //spawn datepickerview
    CGRect datePickerFrame = CGRectMake(0, 40, 0, 0);
    _datePickerView = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
    self.datePickerView.datePickerMode = self.datePickerMode;

    [self.datePickerView setDate:self.selectedDate];

    // To support minimum date
    if (_minimumDate != nil) {
    [self.datePickerView setMinimumDate:self.minimumDate];
    }

    [self.datePickerView addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];

    [self.actionSheet addSubview:self.datePickerView];
    }

  • (void)actionPickerDone {

    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];

    if (nil != self.multipleComponentsData) {
    //send multiple data picker message
    [self.target performSelector:self.action withObject:self.multipleComponentsSeletedIndex];
    }

    else if (nil != self.data) {
    //send data picker message
    [self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex]];
    } else {
    //send date picker message
    [self.target performSelector:self.action withObject:self.selectedDate];
    }

    if (self.convenientObject)
    [self release]; //release convenient object

}

  • (void)actionPickerCancel {
    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    }
  • (BOOL) isViewPortrait {
    UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
    return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }

pragma mark -

pragma mark Callbacks

  • (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 {
    if (self.multipleComponentsData != nil) {
    NSNumber *number = [NSNumber numberWithInt:row];
    [self.multipleComponentsSeletedIndex replaceObjectAtIndex:component withObject:number];
    }

    else {
    self.selectedIndex = row;
    }
    }

pragma mark -

pragma mark UIPickerViewDataSource

  • (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    // For multiple components: if data is an array contains other arrays,
    // each array will represent data in one component
    if (self.multipleComponentsData != nil) {
    return [self.multipleComponentsData count];
    }

    return 1;
    }

  • (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    // For multiple components: if data is an array contains other arrays,
    // each array will represent data in one component
    if (self.multipleComponentsData != nil) {
    return [[self.multipleComponentsData objectAtIndex:component] count];
    }

    // For one section
    return self.data.count;
    }

  • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    // For multiple components: if data is an array contains other arrays,
    // each array will represent data in one component
    if (self.multipleComponentsData != nil) {
    return [[self.multipleComponentsData objectAtIndex:component] objectAtIndex:row];
    }

    // For 1 section
    return [self.data objectAtIndex:row];
    }

// Custom, depend on specific situation
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (self.componentWidths != nil) {
return [[self.componentWidths objectAtIndex:component] intValue];
}

// If componentWidths is nil
return 300;

}

pragma mark -

pragma mark Memory Management

  • (void)dealloc {
    self.actionSheet = nil;

    self.pickerView.delegate = nil;
    self.pickerView.dataSource = nil;
    self.pickerView = nil;

    [self.datePickerView removeTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];
    self.datePickerView = nil;
    self.selectedDate = nil;

    [_multipleComponentsData release];
    [_multipleComponentsSelectedIndex release];
    [_componentWidths release];
    [super dealloc];
    }

@EnD

Blocks

Please add functionality to allow use blocks to handle the selection.

How Do You Use This?

This looks really good and is just what I need to put a date into a UITextfield on the app I'm working on but I can't figure out how I'm supposed to use it. I've not done anything with @Class or at interface before so this is all new to me and the sample doesn't really make it clear. Which files am I supposed to import into my project and what code am I supposed to put into the view controller that's going to use it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.