From 113a5c2d33758b53a5928460fe1438336a69b053 Mon Sep 17 00:00:00 2001 From: hackiftekhar Date: Sat, 17 Mar 2018 17:39:42 +0530 Subject: [PATCH] - Implemented a way to programmatically select specific index path. - Improved handling behavior by replacing some older approaches with newer API's. --- ActionSheetPickerView/ViewController.m | 17 ++- IQActionSheetPickerView.podspec.json | 4 +- .../IQActionSheetPickerView.h | 34 +++-- .../IQActionSheetPickerView.m | 123 ++++++++++-------- 4 files changed, 112 insertions(+), 66 deletions(-) diff --git a/ActionSheetPickerView/ViewController.m b/ActionSheetPickerView/ViewController.m index 7cf42c3..574831f 100644 --- a/ActionSheetPickerView/ViewController.m +++ b/ActionSheetPickerView/ViewController.m @@ -14,6 +14,21 @@ @interface ViewController () @implementation ViewController +- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didChangeRow:(NSInteger)row inComponent:(NSInteger)component +{ + if (pickerView.tag == 2) + { + if (component == 0) + { + [pickerView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[pickerView selectedRowInComponent:0] inSection:1] animated:YES]; + } + else if (component == 1) + { + [pickerView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[pickerView selectedRowInComponent:1] inSection:0] animated:YES]; + } + } +} + -(void)actionSheetPickerView:(IQActionSheetPickerView *)pickerView didSelectTitles:(NSArray *)titles { switch (pickerView.tag) @@ -71,7 +86,7 @@ - (IBAction)onePickerViewClicked:(UIButton *)sender pickerView.titlesForComponents = @[@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"],@[@"AM",@"PM"]]; // pickerView.actionToolbar.tintColor = [UIColor blueAppColor]; - pickerView.selectedTitles = @[@"5", @"PM"]; + pickerView.selectedIndexes = @[@(4), @(1)]; [pickerView show]; diff --git a/IQActionSheetPickerView.podspec.json b/IQActionSheetPickerView.podspec.json index fdb3633..6c6d0cc 100644 --- a/IQActionSheetPickerView.podspec.json +++ b/IQActionSheetPickerView.podspec.json @@ -1,9 +1,9 @@ { "name": "IQActionSheetPickerView", - "version": "1.0.8", + "version": "2.0.0", "source": { "git": "https://github.com/hackiftekhar/IQActionSheetPickerView.git", - "tag": "v1.0.8" + "tag": "v2.0.0" }, "summary": "ActionSheet with UIPickerView", "homepage": "http://github.com/hackiftekhar/IQActionSheetPickerView", diff --git a/IQActionSheetPickerView/IQActionSheetPickerView.h b/IQActionSheetPickerView/IQActionSheetPickerView.h index f573718..d68ef79 100644 --- a/IQActionSheetPickerView/IQActionSheetPickerView.h +++ b/IQActionSheetPickerView/IQActionSheetPickerView.h @@ -58,9 +58,16 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) { @protocol IQActionSheetPickerViewDelegate @optional -- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitles:(nonnull NSArray*)titles; +- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitlesAtIndexes:(nonnull NSArray*)indexes; +- (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectTitles:(nonnull NSArray*)titles __attribute__((deprecated("This is replaced by `actionSheetPickerView:didSelectTitlesAtIndexes`."))); //If you implemented `actionSheetPickerView:didSelectTitlesAtIndexes:` delegate method then this method will not get called. + + - (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didSelectDate:(nonnull NSDate*)date; + + - (void)actionSheetPickerView:(nonnull IQActionSheetPickerView *)pickerView didChangeRow:(NSInteger)row inComponent:(NSInteger)component; + + - (void)actionSheetPickerViewDidCancel:(nonnull IQActionSheetPickerView *)pickerView; - (void)actionSheetPickerViewWillCancel:(nonnull IQActionSheetPickerView *)pickerView; @end @@ -126,14 +133,26 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) { ///----------------------------------------- /*! - selected titles for each component. (Not Animated) + selected indexes for each component. (Not Animated) + */ +@property(nullable, nonatomic, strong) NSArray *selectedIndexes; + +/*! + Select the provided index row for each component. Ignore if actionSheetPickerStyle is IQActionSheetPickerStyleDatePicker. + */ +-(void)setSelectedIndexes:(nonnull NSArray *)selectedIndexes animated:(BOOL)animated; + + +/*! + get selected row in component. */ -@property(nullable, nonatomic, strong) NSArray *selectedTitles; +-(NSInteger)selectedRowInComponent:(NSUInteger)component; /*! - set selected titles for each component. + Select a row in pickerView. */ --(void)setSelectedTitles:(nonnull NSArray *)selectedTitles animated:(BOOL)animated; +-(void)selectRowAtIndexPath:(nonnull NSIndexPath*)indexPath; +-(void)selectRowAtIndexPath:(nonnull NSIndexPath*)indexPath animated:(BOOL)animated; /*! Titles to show for component. For example. @[ @[ @"1", @"2", @"3", ], @[ @"11", @"12", @"13", ], @[ @"21", @"22", @"23", ]]. @@ -158,11 +177,6 @@ typedef NS_ENUM(NSUInteger, IQActionSheetPickerStyle) { */ @property(nullable, nonatomic, strong) UIColor *pickerComponentsColor UI_APPEARANCE_SELECTOR; -/*! - Select the provided index row for each component. Ignore if actionSheetPickerStyle is IQActionSheetPickerStyleDatePicker. - */ --(void)selectIndexes:(nonnull NSArray *)indexes animated:(BOOL)animated; - /*! If YES then it will force to scroll third picker component to pick equal or larger row then the first. */ diff --git a/IQActionSheetPickerView/IQActionSheetPickerView.m b/IQActionSheetPickerView/IQActionSheetPickerView.m index dde1612..c5d5ffa 100644 --- a/IQActionSheetPickerView/IQActionSheetPickerView.m +++ b/IQActionSheetPickerView/IQActionSheetPickerView.m @@ -182,29 +182,47 @@ -(void)pickerDoneClicked:(UIBarButtonItem*)barButton { case IQActionSheetPickerStyleTextPicker: { - NSMutableArray *selectedTitles = [[NSMutableArray alloc] init]; + NSMutableArray *selectedIndexes = [[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]]; - } + [selectedIndexes addObject:@(row)]; } - [self setSelectedTitles:selectedTitles]; + [self setSelectedIndexes:selectedIndexes]; - if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitles:)]) + if ([self.delegate respondsToSelector:@selector(actionSheetPickerView:didSelectTitlesAtIndexes:)]) { - [self.delegate actionSheetPickerView:self didSelectTitles:selectedTitles]; + [self.delegate actionSheetPickerView:self didSelectTitlesAtIndexes:selectedIndexes]; + } + else + { + NSMutableArray *selectedTitles = [[NSMutableArray alloc] init]; + + for (NSUInteger component = 0; component *)selectedIndexes { - [self setSelectedTitles:selectedTitles animated:NO]; + [self setSelectedIndexes:selectedIndexes animated:NO]; } --(NSArray *)selectedTitles +-(NSArray *)selectedIndexes { if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker) { - NSMutableArray *selectedTitles = [[NSMutableArray alloc] init]; - - NSUInteger totalComponent = _pickerView.numberOfComponents; + NSMutableArray *selectedIndexes = [[NSMutableArray alloc] init]; - for (NSInteger component = 0; component selectedRow) - { - id selectTitle = items[selectedRow]; - [selectedTitles addObject:selectTitle]; - } - else - { - [selectedTitles addObject:[NSNull null]]; - } - } + [selectedIndexes addObject:@(row)]; } - return selectedTitles; + return selectedIndexes; } else { @@ -315,40 +314,58 @@ -(NSArray *)selectedTitles } } --(void)setSelectedTitles:(NSArray *)selectedTitles animated:(BOOL)animated +-(void)setSelectedIndexes:(NSArray *)selectedIndexes animated:(BOOL)animated { if (_actionSheetPickerStyle == IQActionSheetPickerStyleTextPicker) { - NSUInteger totalComponent = MIN(selectedTitles.count, _pickerView.numberOfComponents); + NSUInteger totalComponent = MIN(MIN(selectedIndexes.count, _pickerView.numberOfComponents),_titlesForComponents.count); for (NSInteger component = 0; component