madjid / MMPickerView

An easy to use and customizable PickerView for your iOS app.
MIT License
237 stars 59 forks source link

Problem with custom objects, objectToStringConverter and completion #18

Open buguibu opened 10 years ago

buguibu commented 10 years ago

This is my object class:

@class RKObjectMapping;

@interface MVCountryModel : NSObject

@property (strong, nonatomic) NSNumber *countryId;
@property (strong, nonatomic) NSString *countryName;

+ (RKObjectMapping *) responseMapping;

- (int) integerValue;

@end

First of all, i had to implement "integerValue" for displaying the picker in this way:

    [MMPickerView showPickerViewInView:self.view
                           withObjects:_countries
                           withOptions:nil
               objectToStringConverter:^NSString *(id object) {
                   return ((MVCountryModel *)object).countryName;
               }
                            completion:^(id selectedObject) {
                                MVCountryModel *country = (MVCountryModel *)selectedObject;
                                [_labelCountry setText:country.countryName];
                                _selectedCountryId = country.countryId;
                            }];

But when a option where selected the app crash on my completion block because it only receive a NSString not an instance of my custom object class.

For make it work i had to change this method in this way:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  if (self.objectToStringConverter == nil) {
      /* self.onDismissCompletion ([_pickerViewArray objectAtIndex:row]); */
      self.onDismissCompletion (self.objectToStringConverter ([self selectedObject]));
  } else{
      /* self.onDismissCompletion (self.objectToStringConverter ([self selectedObject])); */
      self.onDismissCompletion ([_pickerViewArray objectAtIndex:row]);
  }
}

It looks so simple that i think i'm doing something wrong, but don't! Hope it helps.

osorioabel commented 9 years ago

I was having the same error , and use your solution and work perfect :+1: Thanks