Skip to content

Commit

Permalink
A UIView contain no text rather than nil should be returned when obje…
Browse files Browse the repository at this point in the history
…ct in data is not NSString and do not respond to decription message.
  • Loading branch information
ainopara committed Oct 9, 2015
1 parent fac40f5 commit da32071
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Pickers/ActionSheetStringPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,28 @@ - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleFor
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
id obj = (self.data)[(NSUInteger) row];
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
pickerLabel = [[UILabel alloc] init];
}
id obj = (self.data)[(NSUInteger) row];

NSAttributedString *attributeTitle = nil;
// return the object if it is already a NSString,
// otherwise, return the description, just like the toString() method in Java
// else, return nil to prevent exception
// use the object if it is already a NSString,
// otherwise, use the description, just like the toString() method in Java
// else, use String with no text to ensure this delegate do not return a nil value.

if ([obj isKindOfClass:[NSString class]])
attributeTitle = [[NSAttributedString alloc] initWithString:obj attributes:self.pickerTextAttributes];

if ([obj respondsToSelector:@selector(description)])
attributeTitle = [[NSAttributedString alloc] initWithString:[obj performSelector:@selector(description)] attributes:self.pickerTextAttributes];

if (attributeTitle != nil) {
pickerLabel.attributedText = attributeTitle;
return pickerLabel;
if (attributeTitle == nil) {
attributeTitle = [[NSAttributedString alloc] initWithString:@"" attributes:self.pickerTextAttributes];
}
return nil;
pickerLabel.attributedText = attributeTitle;
return pickerLabel;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
Expand Down

0 comments on commit da32071

Please sign in to comment.