ActionSheetCustomPicker.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // ActionSheetPicker.m
  3. // ActionSheetPicker
  4. //
  5. // Created by on 13/03/2012.
  6. // Copyright (c) 2012 Club 15CC. All rights reserved.
  7. //
  8. #import "ActionSheetCustomPicker.h"
  9. @interface ActionSheetCustomPicker ()
  10. @property(nonatomic, strong) NSArray *initialSelections;
  11. @end
  12. @implementation ActionSheetCustomPicker
  13. /////////////////////////////////////////////////////////////////////////
  14. #pragma mark - Init
  15. /////////////////////////////////////////////////////////////////////////
  16. - (instancetype)initWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin
  17. {
  18. return [self initWithTitle:title delegate:delegate
  19. showCancelButton:showCancelButton origin:origin
  20. initialSelections:nil];
  21. }
  22. + (instancetype)showPickerWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin
  23. {
  24. return [self showPickerWithTitle:title delegate:delegate showCancelButton:showCancelButton origin:origin
  25. initialSelections:nil ];
  26. }
  27. - (instancetype)initWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin initialSelections:(NSArray *)initialSelections
  28. {
  29. if ( self = [self initWithTarget:nil successAction:nil cancelAction:nil origin:origin] )
  30. {
  31. self.title = title;
  32. self.hideCancel = !showCancelButton;
  33. NSAssert(delegate, @"Delegate can't be nil");
  34. _delegate = delegate;
  35. if (initialSelections)
  36. self.initialSelections = [[NSArray alloc] initWithArray:initialSelections];
  37. }
  38. return self;
  39. }
  40. /////////////////////////////////////////////////////////////////////////
  41. + (instancetype)showPickerWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin initialSelections:(NSArray *)initialSelections
  42. {
  43. ActionSheetCustomPicker *picker = [[ActionSheetCustomPicker alloc] initWithTitle:title delegate:delegate
  44. showCancelButton:showCancelButton origin:origin
  45. initialSelections:initialSelections];
  46. [picker showActionSheetPicker];
  47. return picker;
  48. }
  49. /////////////////////////////////////////////////////////////////////////
  50. #pragma mark - AbstractActionSheetPicker fulfilment
  51. /////////////////////////////////////////////////////////////////////////
  52. - (UIView *)configuredPickerView
  53. {
  54. CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
  55. UIPickerView *pv = [[UIPickerView alloc] initWithFrame:pickerFrame];
  56. self.pickerView = pv;
  57. // Default to our delegate being the picker's delegate and datasource
  58. pv.delegate = _delegate;
  59. pv.dataSource = _delegate;
  60. pv.showsSelectionIndicator = YES;
  61. if ( self.initialSelections )
  62. {
  63. NSAssert(pv.numberOfComponents == self.initialSelections.count, @"Number of sections not match");
  64. for (NSUInteger i = 0; i < [self.initialSelections count]; i++)
  65. {
  66. NSInteger row = [(NSNumber *) self.initialSelections[i] integerValue];
  67. NSAssert([pv numberOfRowsInComponent:i] > row, @"Number of sections not match");
  68. [pv selectRow:row inComponent:i animated:NO];
  69. // Strangely, the above selectRow:inComponent:animated: will not call
  70. // pickerView:didSelectRow:inComponent: automatically, so we manually call it.
  71. [pv reloadAllComponents];
  72. }
  73. }
  74. // Allow the delegate to override and set additional configs
  75. //to backward compatibility:
  76. if ( [_delegate respondsToSelector:@selector(actionSheetPicker:configurePickerView:)] )
  77. {
  78. [_delegate actionSheetPicker:self configurePickerView:pv];
  79. }
  80. return pv;
  81. }
  82. /////////////////////////////////////////////////////////////////////////
  83. - (void)notifyTarget:(id)target didSucceedWithAction:(SEL)successAction origin:(id)origin
  84. {
  85. // Ignore parent args and just notify the delegate
  86. if ( [_delegate respondsToSelector:@selector(actionSheetPickerDidSucceed:origin:)] )
  87. {
  88. [_delegate actionSheetPickerDidSucceed:self origin:origin];
  89. }
  90. }
  91. /////////////////////////////////////////////////////////////////////////
  92. - (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin
  93. {
  94. // Ignore parent args and just notify the delegate
  95. if ( [_delegate respondsToSelector:@selector(actionSheetPickerDidCancel:origin:)] )
  96. {
  97. [_delegate actionSheetPickerDidCancel:self origin:origin];
  98. }
  99. }
  100. @end