Instantly share code, notes, and snippets.

Embed
What would you like to do?
MyPickerView
#import <UIKit/UIKit.h>
@interface MyPickerView : UIView
<UIPickerViewDataSource, UIPickerViewDelegate>
{
UIToolbar *toolbar;
UIPickerView *picker;
void (^completion)(NSString *selected);
NSArray *items;
}
- (void)setSelectedRow:(NSInteger)row;
- (NSInteger)selectedRow;
- (void)setItems:(NSArray *)array;
- (void)showInWindow:(UIWindow *)window completion:(void (^)(NSString *selected))aCompletion;
- (void)hide;
@end
#import "MyPickerView.h"
@implementation MyPickerView
- (id)init
{
self = [super initWithFrame:[UIScreen mainScreen].bounds];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
picker = [[UIPickerView alloc] init];
picker.showsSelectionIndicator = YES;
picker.delegate = self;
picker.dataSource = self;
CGRect f = picker.frame;
f.origin.y = 480 - f.size.height;
picker.frame = f;
[self addSubview:picker];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
f = toolbar.frame;
f.origin.y = picker.frame.origin.y - 44;
toolbar.frame = f;
[self addSubview:toolbar];
UIBarButtonItem *doneItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneTapped:)];
[doneItem autorelease];
UIBarButtonItem *cancelItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(hide)];
[cancelItem autorelease];
NSArray *buttonItems = [NSArray arrayWithObjects:
cancelItem,
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease],
doneItem, nil];
[toolbar setItems:buttonItems animated:NO];
items = [[NSArray array] retain];
}
return self;
}
- (void)setDelegate:(id)aDelegate
{
picker.delegate = aDelegate;
}
- (void)setSelectedRow:(NSInteger)row
{
[picker selectRow:row inComponent:0 animated:NO];
}
- (NSInteger)selectedRow
{
return [picker selectedRowInComponent:0];
}
- (void)setItems:(NSArray *)array
{
if (items) {
[items release]; items = nil;
}
items = [array retain];
}
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [items count];
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [items objectAtIndex:row];
}
#pragma mark -
- (void)showInWindow:(UIWindow *)window completion:(void (^)(NSString *selected))aCompletion;
{
completion = [aCompletion copy];
[picker reloadAllComponents];
CGRect f = toolbar.frame;
f.origin.y += 480;
toolbar.frame = f;
f = picker.frame;
f.origin.y += 480;
picker.frame = f;
[window addSubview:self];
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = toolbar.frame;
frame.origin.y -= 480;
toolbar.frame = frame;
frame = picker.frame;
frame.origin.y -= 480;
picker.frame = frame;
}];
}
- (void)hide
{
[completion release];
completion = nil;
[UIView animateWithDuration:0.3
animations:^{
CGRect frame = toolbar.frame;
frame.origin.y += 480;
toolbar.frame = frame;
frame = picker.frame;
frame.origin.y += 480;
picker.frame = frame;
}
completion:^ (BOOL finished) {
[self removeFromSuperview];
CGRect f = toolbar.frame;
f.origin.y -= 480;
toolbar.frame = f;
f = picker.frame;
f.origin.y -= 480;
picker.frame = f;
}];
}
- (void)doneTapped:(id)sender
{
completion([items objectAtIndex:[picker selectedRowInComponent:0]]);
[self hide];
}
- (void)dealloc
{
[toolbar release];
[picker release];
[completion release];
[items release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment