Monday, August 4, 2014

iOS date picker with a done button

I had to create a date picker with a done button. I am sharing that experience with this post. These are the steps I used.
  1. Create a UIView and add UIDatePicker to it
  2. Add a done buttton
  3. Add ability to assign a selector to done button. So we can respond to the click event

This is the code for the implementation
DatePicker.h file
//
//  DatePicker.h

#import 

@interface DatePicker : UIView

@property (nonatomic, assign, readonly) UIDatePicker *datePicker;
@property (nonatomic, readwrite) float pickerHeight;

- (void) setDatePickerMode: (UIDatePickerMode) mode;
- (void) addTargetAndSelectorForDoneButton: (id) target action: (SEL) action;

- (id) initWithFrame: (CGRect) frame withHeight:(float)height;

@end


DatePicker.m file

#import "DatePicker.h"

@interface DatePicker ()

@property (nonatomic, readwrite) UIDatePicker *datePicker;
@property (nonatomic, assign) id doneTarget;
@property (nonatomic, assign) SEL doneHandler;

@end

@implementation DatePicker

@synthesize datePicker = _datePicker;
@synthesize pickerHeight = _pickerHeight;
@synthesize doneTarget = _doneTarget;
@synthesize doneHandler = _doneHandler;

- (id) initWithFrame: (CGRect) frame withHeight:(float)height{
    if ((self = [super initWithFrame: frame])) {
        self.backgroundColor = [UIColor clearColor];
        _pickerHeight = height;
        
        UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame: CGRectMake(0, _pickerHeight, frame.size.width, frame.size.height - _pickerHeight)];
        [self addSubview: picker];
        
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, frame.size.width, _pickerHeight)];
        toolbar.barStyle = UIBarStyleBlackOpaque;
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Done" style: UIBarButtonItemStyleBordered target: self action: @selector(donePressed)];
        UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        toolbar.items = [NSArray arrayWithObjects:flexibleSpace, doneButton, nil];
        
        [self addSubview: toolbar];
        
        self.datePicker = picker;
        picker.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
        
        self.autoresizesSubviews = YES;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    }
    return self;
}

- (void) setDatePickerMode: (UIDatePickerMode) mode {
    self.datePicker.datePickerMode = mode;
}

- (void) donePressed {
    if (self.doneTarget) {
        [self.doneTarget performSelector:self.doneHandler withObject:nil afterDelay:0];
    }
}

- (void) addTargetAndSelectorForDoneButton: (id) target action: (SEL) action {
    self.doneTarget = target;
    self.doneHandler = action;
}


No comments:

Post a Comment