diff --git a/Demo/Classes/MMTestViewController.m b/Demo/Classes/MMTestViewController.m index 7432c73..a8bdc83 100644 --- a/Demo/Classes/MMTestViewController.m +++ b/Demo/Classes/MMTestViewController.m @@ -103,11 +103,16 @@ - (IBAction)showPickerViewButtonPressed:(id)sender { MMvalueY: @3, MMselectedObject:_selectedString, MMtextAlignment:@1} + selected:^(NSString *selectedString) { + + _label.text = selectedString; + _selectedString = selectedString; + } completion:^(NSString *selectedString) { _label.text = selectedString; _selectedString = selectedString; - }]; + }]; diff --git a/MMPickerView/MMPickerView.h b/MMPickerView/MMPickerView.h index abbf7c1..70e09b7 100644 --- a/MMPickerView/MMPickerView.h +++ b/MMPickerView/MMPickerView.h @@ -18,19 +18,22 @@ extern NSString * const MMselectedObject; extern NSString * const MMtoolbarBackgroundImage; extern NSString * const MMtextAlignment; extern NSString * const MMshowsSelectionIndicator; +extern NSString * const MMspaceBottom; -@interface MMPickerView: UIView +@interface MMPickerView: UIView +(void)showPickerViewInView: (UIView *)view withStrings: (NSArray *)strings withOptions: (NSDictionary *)options + selected: (void (^)(NSString *))selected completion: (void(^)(NSString *selectedString))completion; +(void)showPickerViewInView: (UIView *)view withObjects: (NSArray *)objects withOptions: (NSDictionary *)options objectToStringConverter: (NSString *(^)(id object))converter - completion: (void(^)(id selectedObject))completion; + selected: (void (^)(NSString *))selected + completion: (void(^)(id selectedObject))completion; +(void)dismissWithCompletion: (void(^)(NSString *))completion; diff --git a/MMPickerView/MMPickerView.m b/MMPickerView/MMPickerView.m index 0986641..ebfbb45 100644 --- a/MMPickerView/MMPickerView.m +++ b/MMPickerView/MMPickerView.m @@ -18,6 +18,7 @@ NSString * const MMtoolbarBackgroundImage = @"toolbarBackgroundImage"; NSString * const MMtextAlignment = @"textAlignment"; NSString * const MMshowsSelectionIndicator = @"showsSelectionIndicator"; +NSString * const MMspaceBottom = @"spaceBottom"; @interface MMPickerView () @@ -37,6 +38,7 @@ @interface MMPickerView () @property (nonatomic, assign) CGFloat yValueFromTop; @property (nonatomic, assign) NSInteger pickerViewTextAlignment; @property (nonatomic, assign) BOOL pickerViewShowsSelectionIndicator; +@property (copy) void (^onSelect)(NSString *); @property (copy) void (^onDismissCompletion)(NSString *); @property (copy) NSString *(^objectToStringConverter)(id object); @@ -59,6 +61,7 @@ + (MMPickerView*)sharedView { +(void)showPickerViewInView:(UIView *)view withStrings:(NSArray *)strings withOptions:(NSDictionary *)options + selected: (void (^)(NSString *))selected completion:(void (^)(NSString *))completion{ [[self sharedView] initializePickerViewInView:view @@ -66,6 +69,7 @@ +(void)showPickerViewInView:(UIView *)view withOptions:options]; [[self sharedView] setPickerHidden:NO callBack:nil]; + [self sharedView].onSelect = selected; [self sharedView].onDismissCompletion = completion; [view addSubview:[self sharedView]]; @@ -75,13 +79,17 @@ +(void)showPickerViewInView:(UIView *)view withObjects:(NSArray *)objects withOptions:(NSDictionary *)options objectToStringConverter:(NSString *(^)(id))converter + selected: (void (^)(NSString *))selected completion:(void (^)(id))completion { [self sharedView].objectToStringConverter = converter; + [self sharedView].onSelect = selected; [self sharedView].onDismissCompletion = completion; + [[self sharedView] initializePickerViewInView:view withArray:objects withOptions:options]; + [[self sharedView] setPickerHidden:NO callBack:nil]; [view addSubview:[self sharedView]]; @@ -177,10 +185,19 @@ -(void)initializePickerViewInView: (UIView *)view //Whole screen with PickerView and a dimmed background _pickerViewContainerView = [[UIView alloc] initWithFrame:view.bounds]; [_pickerViewContainerView setBackgroundColor: [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]]; + + if (options[MMspaceBottom] != nil) { + + CGRect customFrame = _pickerViewContainerView.frame; + customFrame.size.height -= [options[MMspaceBottom] floatValue]; + + _pickerViewContainerView.frame = customFrame; + } + [self addSubview:_pickerViewContainerView]; //PickerView Container with top bar - _pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, 320.0, 260.0)]; + _pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, view.frame.size.width, 260.0)]; //Default Color Values (if colors == nil) @@ -210,22 +227,9 @@ -(void)initializePickerViewInView: (UIView *)view } _pickerViewFont = pickerViewFont; - /* - //ToolbackBackgroundImage - Clear Color - if (toolbarBackgroundImage!=nil) { - //Top bar imageView - _pickerTopBarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, _pickerContainerView.frame.size.width, 44.0)]; - //[_pickerContainerView addSubview:_pickerTopBarImageView]; - _pickerTopBarImageView.image = toolbarBackgroundImage; - [_pickerViewToolBar setHidden:YES]; - - } - */ - _pickerContainerView.backgroundColor = pickerViewBackgroundColor; [_pickerViewContainerView addSubview:_pickerContainerView]; - //Content of pickerContainerView //Top bar view @@ -238,16 +242,11 @@ -(void)initializePickerViewInView: (UIView *)view [_pickerContainerView addSubview:_pickerViewToolBar]; CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; - //NSLog(@"%f",iOSVersion); - if (iOSVersion < 7.0) { - _pickerViewToolBar.tintColor = toolbarBackgroundColor; - //[_pickerViewToolBar setBackgroundColor:toolbarBackgroundColor]; + _pickerViewToolBar.tintColor = toolbarBackgroundColor; }else{ [_pickerViewToolBar setBackgroundColor:toolbarBackgroundColor]; - //_pickerViewToolBar.tintColor = toolbarBackgroundColor; - #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 _pickerViewToolBar.barTintColor = toolbarBackgroundColor; #endif @@ -263,23 +262,12 @@ -(void)initializePickerViewInView: (UIView *)view _pickerViewToolBar.items = @[flexibleSpace, _pickerViewBarButtonItem]; [_pickerViewBarButtonItem setTintColor:buttonTextColor]; - //[_pickerViewBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Neue" size:23.0], UITextAttributeFont,nil] forState:UIControlStateNormal]; - - /* - _pickerDoneButton = [[UIButton alloc] initWithFrame:CGRectMake(_pickerContainerView.frame.size.width - 80.0, 10.0, 60.0, 24.0)]; - [_pickerDoneButton setTitle:@"Done" forState:UIControlStateNormal]; - [_pickerContainerView addSubview:_pickerDoneButton]; - [_pickerDoneButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; - */ - //Add pickerView - _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 320.0, 216.0)]; + _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, view.frame.size.width, 216.0)]; [_pickerView setDelegate:self]; [_pickerView setDataSource:self]; [_pickerView setShowsSelectionIndicator: _pickerViewShowsSelectionIndicator];//YES]; [_pickerContainerView addSubview:_pickerView]; - - //[self.pickerViewContainerView setAlpha:0.0]; [_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))]; //Set selected row @@ -310,9 +298,9 @@ - (NSString *)pickerView: (UIPickerView *)pickerView -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (self.objectToStringConverter == nil) { - self.onDismissCompletion ([_pickerViewArray objectAtIndex:row]); + self.onSelect ([_pickerViewArray objectAtIndex:row]); } else{ - self.onDismissCompletion (self.objectToStringConverter ([self selectedObject])); + self.onSelect (self.objectToStringConverter ([self selectedObject])); } } @@ -334,10 +322,6 @@ - (UIView *)pickerView:(UIPickerView *)pickerView CGRect frame = CGRectMake(0.0, 0.0, 292.0, 44.0); customPickerView = [[UIView alloc] initWithFrame: frame]; -// UIImageView *patternImageView = [[UIImageView alloc] initWithFrame:frame]; -// patternImageView.image = [[UIImage imageNamed:@"texture"] resizableImageWithCapInsets:UIEdgeInsetsZero]; -// [customPickerView addSubview:patternImageView]; - if (_yValueFromTop == 0.0f) { _yValueFromTop = 3.0; } diff --git a/README.md b/README.md index a66fbf8..83575ba 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,16 @@ You can show the PickerView: ```objective-c +(void)showPickerViewInView: (UIView *)view withStrings: (NSArray *)strings - withOptions: (NSDictionary *)options + withOptions: (NSDictionary *)options, + selected: (void(^)(NSString *selectedString))selected completion: (void(^)(NSString *selectedString))completion; +(void)showPickerViewInView: (UIView *)view withObjetcs: (NSArray *)objects withOptions: (NSDictionary *)options objectToStringConverter: (NSString *(^)(id object))converter - completion: (void(^)(id selectedObject))completion; + selected: (void(^)(NSString *selectedString))selected + completion: (void(^)(id selectedObject))completion; ``` @@ -59,10 +61,14 @@ You can show the PickerView: [MMPickerView showPickerViewInView:self.view withStrings:strings withOptions:nil - completion:^(NSString *selectedString) { - //selectedString is the return value which you can use as you wish - self.label.text = selectedString; - }]; + selected:^(NSString *selectedString) { + //selectedString is the return value when the selected item change + self.label.text = selectedString; + } + completion:^(NSString *selectedString) { + //selectedString is the return value after user hit Done button + self.label.text = selectedString; + }]; ``` #### Example 2 - Show with an array of objects. @@ -73,16 +79,19 @@ You can show the PickerView: [MMPickerView showPickerViewInView:self.view withObjects:objects withOptions:nil - objectToStringConverter:^NSString *(id object) { - //This is where you convert your object and return a string, for eg. return person.name; - return [object description]; - - } - completion:^(id selectedObject) { - //The selected object is returned, and you can use the value as you wish - //For example: self.label.text = person.name; - self.label.text = [selectedObject description]; - }]; + objectToStringConverter:^NSString *(id object) { + //This is where you convert your object and return a string, for eg. return person.name; + return [object description]; + } + selected:^(id selectedObject) { + //selectedObject: The selected object when the selected item change + self.label.text = [selectedObject description]; + } + completion:^(id selectedObject) { + //selectedObject: The selected object after user hit Done button + //For example: self.label.text = person.name; + self.label.text = [selectedObject description]; + }]; ``` ### Customizing MMPickerView @@ -100,6 +109,7 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`. - `MMselectedObject` - `id` - `MMtoolbarBackgroundImage` - `UIImage` - `MMtextAlignment` - `NSNumber` +- `MMspaceBottom` - `NSNumber` ```objective-c /* @@ -113,6 +123,7 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`. MMselectedObject - id - The selected object presented in the PickerView, an object from the array, for eg. [yourArray objectAtIndex:0]; MMtoolbarBackgroundImage - UIImage - The background image of the toolbar (320 x 44 for non retina, 640 x 88 for retina) MMtextAlignment - NSNumber - The text alignment of the labels in the PickerView, @0 for Left, @1 for Center, @2 for Right + MMspaceBottom - NSNumber - If the picker view is combined with a bottom bar (such as tabbar) set a MMspaceBottom with the height of bar. */ ``` @@ -132,8 +143,12 @@ Both show methods use a `NSDictionary` to set the options of the `MMPickerView`. [MMPickerView showPickerViewInView:self.view withStrings:strings withOptions:options + selected:^(NSString *selectedString) { + //selectedString is the return value when the selected item change + self.label.text = selectedString; + } completion:^(NSString *selectedString) { - //selectedString is the return value which you can use as you wish + //selectedString is the return value after user hit Done button self.label.text = selectedString; }]; ``` @@ -173,11 +188,13 @@ A useful feature is to let the PickerView select whatever was selected last time [MMPickerView showPickerViewInView:self.view withStrings:strings withOptions:@{selectedObject:_selectedString} - completion:^(NSString *selectedString) { - - _label.text = selectedString; - _selectedString = selectedString; - }]; + selected:^(NSString *selectedString) { + _label.text = selectedString; + } + completion:^(NSString *selectedString) { + _label.text = selectedString; + _selectedString = selectedString; + }]; }