// // IQActionSheetPickerView.m // https://github.com/hackiftekhar/IQActionSheetPickerView // Copyright (c) 2013-14 Iftekhar Qurashi. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #import "IQActionSheetPickerView.h" #import #import "IQActionSheetViewController.h" NSString * const kIQActionSheetAttributesForNormalStateKey = @"kIQActionSheetAttributesForNormalStateKey"; /// Identifies an attributed string of the toolbar title for highlighted state. NSString * const kIQActionSheetAttributesForHighlightedStateKey = @"kIQActionSheetAttributesForHighlightedStateKey"; @interface IQActionSheetPickerView () { UIPickerView *_pickerView; UIDatePicker *_datePicker; } @property(nonatomic,strong) NSArray* selectArray; @property(nonatomic, strong) IQActionSheetViewController *actionSheetController; @end @implementation IQActionSheetPickerView @synthesize actionSheetPickerStyle = _actionSheetPickerStyle; @synthesize titlesForComponents = _titlesForComponents; @synthesize widthsForComponents = _widthsForComponents; @synthesize isRangePickerView = _isRangePickerView; @synthesize delegate = _delegate; @synthesize date = _date; -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate { CGRect rect = [[UIScreen mainScreen] bounds]; rect.size.height = 216+44; self = [super initWithFrame:rect]; if (self) { self.clickBackHide = YES; self.pickBkgColor = HexColorFromRGBA(0x000000,0.5); //UIToolbar { self.actionToolbar = [[IQActionSheetToolbar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 44)]; self.actionToolbar.barStyle = UIBarStyleDefault; self.actionToolbar.cancelButton.target = self; self.actionToolbar.cancelButton.action = @selector(pickeryoupaifcancelClicked:); self.actionToolbar.doneButton.target = self; self.actionToolbar.doneButton.action = @selector(pickerDoneClicked:); self.actionToolbar.titleButton.title = title; [self addSubview:self.actionToolbar]; } //UIPickerView { _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_actionToolbar.frame) , CGRectGetWidth(_actionToolbar.frame), 216)]; _pickerView.backgroundColor = [UIColor whiteColor]; [_pickerView setShowsSelectionIndicator:NO]; [_pickerView setDelegate:self]; [_pickerView setDataSource:self]; [self addSubview:_pickerView]; } //UIDatePicker { _datePicker = [[UIDatePicker alloc] initWithFrame:_pickerView.frame]; [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged]; _datePicker.frame = _pickerView.frame; [_datePicker setDatePickerMode:UIDatePickerModeDate]; [self addSubview:_datePicker]; } //Initial settings { self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8]; [self setFrame:CGRectMake(0, 0, CGRectGetWidth(_pickerView.frame), CGRectGetMaxY(_pickerView.frame))]; [self setActionSheetPickerStyle:IQActionSheetPickerStyleTextPicker]; self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth; _actionToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; _pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; _datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth; } } _delegate = delegate; return self; } -(void)setActionSheetPickerStyle:(IQActionSheetPickerStyle)actionSheetPickerStyle { _actionSheetPickerStyle = actionSheetPickerStyle; switch (actionSheetPickerStyle) { case IQActionSheetPickerStyleTextPicker: [_pickerView setHidden:NO]; [_datePicker setHidden:YES]; break; case IQActionSheetPickerStyleDatePicker: [_pickerView setHidden:YES]; [_datePicker setHidden:NO]; [_datePicker setDatePickerMode:UIDatePickerModeDate]; break; case IQActionSheetPickerStyleDateTimePicker: [_pickerView setHidden:YES]; [_datePicker setHidden:NO]; [_datePicker setDatePickerMode:UIDatePickerModeDateAndTime]; break; case IQActionSheetPickerStyleTimePicker: [_pickerView setHidden:YES]; [_datePicker setHidden:NO]; [_datePicker setDatePickerMode:UIDatePickerModeTime]; break; default: break; } } /** * Set Picker View Background Color * * @param pickerViewBackgroundColor Picker view custom background color */ -(void)setPickerViewBackgroundColor:(UIColor *)pickerViewBackgroundColor{ _pickerView.backgroundColor = pickerViewBackgroundColor; } /** * Set Cancel Button Title Attributes * * @param cancelButtonAttributes Cancel Button Title Attributes */ -(void)setCancelButtonAttributes:(NSDictionary *)cancelButtonAttributes{ id attributesForCancelButtonNormalState = [cancelButtonAttributes objectForKey:kIQActionSheetAttributesForNormalStateKey]; if (attributesForCancelButtonNormalState != nil && [attributesForCancelButtonNormalState isKindOfClass:[NSDictionary class]]) { [_actionToolbar.cancelButton setTitleTextAttributes:(NSDictionary *)attributesForCancelButtonNormalState forState:UIControlStateNormal]; } id attributesForCancelButtonnHighlightedState = [cancelButtonAttributes objectForKey: kIQActionSheetAttributesForHighlightedStateKey]; if (attributesForCancelButtonnHighlightedState != nil && [attributesForCancelButtonnHighlightedState isKindOfClass:[NSDictionary class]]) { [_actionToolbar.cancelButton setTitleTextAttributes:(NSDictionary *)attributesForCancelButtonnHighlightedState forState:UIControlStateHighlighted]; } } /** * Set Done Button Title Attributes * * @param cancelButtonAttributes Done Button Title Attributes */ -(void)setDoneButtonAttributes:(NSDictionary *)doneButtonAttributes{ id attributesForDoneButtonNormalState = [doneButtonAttributes objectForKey:kIQActionSheetAttributesForNormalStateKey]; if (attributesForDoneButtonNormalState != nil && [attributesForDoneButtonNormalState isKindOfClass:[NSDictionary class]]) { [_actionToolbar.doneButton setTitleTextAttributes:(NSDictionary *)attributesForDoneButtonNormalState forState:UIControlStateNormal]; } id attributesForDoneButtonnHighlightedState = [doneButtonAttributes objectForKey: kIQActionSheetAttributesForHighlightedStateKey]; if (attributesForDoneButtonnHighlightedState != nil && [attributesForDoneButtonnHighlightedState isKindOfClass:[NSDictionary class]]) { [_actionToolbar.doneButton setTitleTextAttributes:(NSDictionary *)attributesForDoneButtonnHighlightedState forState:UIControlStateHighlighted]; } } /** * Set Action Bar Color * * @param barColor Custom color for toolBar */ -(void)setToolbarTintColor:(UIColor *)toolbarTintColor{ _toolbarTintColor = toolbarTintColor; [_actionToolbar setBarTintColor:toolbarTintColor]; } /** * Set Action Tool Bar Button Color * * @param buttonColor Custom color for toolBar button */ -(void)setToolbarButtonColor:(UIColor *)toolbarButtonColor{ _toolbarButtonColor = toolbarButtonColor; [_actionToolbar setTintColor:toolbarButtonColor]; } /*! Font for the UIPickerView */ - (void)setTitleFont:(UIFont *)titleFont { _titleFont = titleFont; _actionToolbar.titleButton.font = titleFont; } /*! * Color for the UIPickerView */ - (void)setTitleColor:(UIColor *)titleColor { _titleColor = titleColor; _actionToolbar.titleButton.titleColor = titleColor; } #pragma mark - Done/Cancel -(void)pickeryoupaifcancelClicked:(UIBarButtonItem*)barButton { if ([self.delegate respondsToSelector:@selector(actionSheetPickerViewWillCancel:)]) { [self.delegate actionSheetPickerViewWillCancel:self]; } [self dismissWithCompletion:^{ if ([self.delegate respondsToSelector:@selector(actionSheetPickerViewDidCancel:)]) { [self.delegate actionSheetPickerViewDidCancel:self]; } }]; } -(void)pickerDoneClicked:(UIBarButtonItem*)barButton { switch (_actionSheetPickerStyle) { case IQActionSheetPickerStyleTextPicker: { NSMutableArray *selectedTitles = [[NSMutableArray alloc] init]; for (NSInteger component = 0; component<_pickerView.numberOfComponents; component++) { NSInteger row = [_pickerView selectedRowInComponent:component]; if (row!= -1) { [selectedTitles addObject:_titlesForComponents[component][row]]; } else { [selectedTitles addObject:[NSNull null]]; } } [self setSelectedTitles:selectedTitles]; if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitles:)]) { [self.delegate actionSheetPickerView:self didSelectTitles:selectedTitles]; } } break; case IQActionSheetPickerStyleDatePicker: case IQActionSheetPickerStyleDateTimePicker: case IQActionSheetPickerStyleTimePicker: { [self setDate:_datePicker.date]; [self setSelectedTitles:@[_datePicker.date]]; if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectDate:)]) { [self.delegate actionSheetPickerView:self didSelectDate:_datePicker.date]; } } default: break; } [self dismiss]; } #pragma mark - IQActionSheetPickerStyleDatePicker / IQActionSheetPickerStyleDateTimePicker / IQActionSheetPickerStyleTimePicker -(void)dateChanged:(UIDatePicker*)datePicker { [self sendActionsForControlEvents:UIControlEventValueChanged]; } -(void) setDate:(NSDate *)date { [self setDate:date animated:NO]; } -(void)setDate:(NSDate *)date animated:(BOOL)animated { _date = date; if (_date != nil) [_datePicker setDate:_date animated:animated]; } -(void)setMinimumDate:(NSDate *)minimumDate { _minimumDate = minimumDate; _datePicker.minimumDate = minimumDate; } -(void)setMaximumDate:(NSDate *)maximumDate { _maximumDate = maximumDate; _datePicker.maximumDate = maximumDate; } #pragma mark - IQActionSheetPickerStyleTextPicker -(void)reloadComponent:(NSInteger)component { [_pickerView reloadComponent:component]; } -(void)reloadAllComponents { [_pickerView reloadAllComponents]; } -(void)setSelectedTitles:(NSArray *)selectedTitles { [self setSelectedTitles:selectedTitles animated:NO]; } -(NSArray *)selectedTitles { if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker) { NSMutableArray *selectedTitles = [[NSMutableArray alloc] init]; NSUInteger totalComponent = _pickerView.numberOfComponents; for (NSInteger component = 0; component selectedRow) { id selectTitle = items[selectedRow]; [selectedTitles addObject:selectTitle]; } else { [selectedTitles addObject:[NSNull null]]; } } } return selectedTitles; } else { return nil; } } -(void)setSelectedTitles:(NSArray *)selectedTitles animated:(BOOL)animated { if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker) { NSUInteger totalComponent = MIN(selectedTitles.count, _pickerView.numberOfComponents); for (NSInteger component = 0; component