DatePickerButton.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // CustomButton.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2013. 12. 16..
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import "CommonUtil.h"
  9. #import "DatePickerButton.h"
  10. #import "CustomDatePopupView.h"
  11. #import "UIViewController-ActionSheetSimulation.h"
  12. #define kfToolBarHeight 44.0f
  13. #define ksEmptyString @""
  14. #define kUIBtnTintColor RGBCOLOR(46, 141, 205)
  15. @interface DatePickerButtonDatePicker : UIDatePicker {
  16. BOOL changed;
  17. }
  18. @end
  19. @implementation DatePickerButtonDatePicker
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. self.layer.backgroundColor = [UIColor whiteColor].CGColor;
  23. }
  24. return self;
  25. }
  26. - (void)awakeFromNib {
  27. self.layer.backgroundColor = [UIColor whiteColor].CGColor;
  28. }
  29. - (void)addSubview:(UIView *)view {
  30. if (!changed) {
  31. changed = YES;
  32. [self setValue:kUIBtnTintColor forKey:@"textColor"]; //TODO : DatePickerButton change color
  33. }
  34. [super addSubview:view];
  35. }
  36. @end
  37. @interface DatePickerButton () {
  38. DatePickerButtonDatePicker *_datePicker;
  39. UIToolbar *_actionToolbar;
  40. NSDateFormatter *_dateFormmater;
  41. UIView *_simulatedActionSheetView;
  42. CustomDatePopupView *_popDate;
  43. }
  44. @end
  45. @implementation DatePickerButton
  46. - (id)initWithCoder:(NSCoder *)aDecoder {
  47. if (self = [super initWithCoder:aDecoder]) {
  48. _datePicker = [[DatePickerButtonDatePicker alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 216)];
  49. }
  50. return self;
  51. }
  52. - (void)awakeFromNib {
  53. [super awakeFromNib];
  54. [self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
  55. }
  56. #pragma mark - UIActionSheet Delegate
  57. - (void)didTouchButton {
  58. if (!_isHidePicker) {
  59. [self showPicker];
  60. }
  61. else {
  62. [self showPopUp];
  63. }
  64. }
  65. - (void)generateActionSheet {
  66. [_datePicker setDatePickerMode:UIDatePickerModeDate];
  67. _dateFormmater = [CommonUtil dateFormatter];
  68. // if (_pickerMode == DatePickerModeFull) {
  69. // [_dateFormmater setDateFormat:@"yyyy/MM/dd"];
  70. // } else if (_pickerMode == DatePickerModeYear) {
  71. // [_dateFormmater setDateFormat:@"yyyy"];
  72. // }
  73. UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"취소" style:UIBarButtonItemStylePlain target:self action:@selector(pickerCancelPressed:)];
  74. UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  75. UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  76. fixedSpace.width = 10;
  77. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"선택" style:UIBarButtonItemStylePlain target:self action:@selector(pickerDonePressed:)];
  78. if (IOS_VERSION >= 7.0f) {
  79. cancelButton.tintColor = kUIBtnTintColor;
  80. doneBtn.tintColor = kUIBtnTintColor;
  81. }
  82. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  83. _actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, kfToolBarHeight)];
  84. _actionToolbar.barStyle = UIBarStyleDefault;
  85. [_actionToolbar setItems:[NSArray arrayWithObjects:fixedSpace, cancelButton, flexSpace, doneBtn, fixedSpace, nil] animated:YES];
  86. _simulatedActionSheetView = [[CommonUtil currentViewController] actionSheetSimulationWithPickerView:_datePicker withToolbar:_actionToolbar];
  87. }
  88. - (void)showPopUp {
  89. if (_popDate == nil) {
  90. _popDate = [[CustomDatePopupView alloc] initWithTitle:_poupTitle];
  91. }
  92. [_popDate showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  93. if (buttonIndex == 0) {//OK
  94. NSLog(@"Input Date : %@.%@.%@", _popDate.txtYear.text, _popDate.txtMonth.text, _popDate.txtDay.text);
  95. }
  96. }];
  97. }
  98. - (void)showPicker {
  99. [[CommonUtil currentViewController].view endEditing:YES];
  100. if (!_simulatedActionSheetView) {
  101. [self generateActionSheet];
  102. }
  103. [[CommonUtil currentViewController] showActionSheetSimulation:_simulatedActionSheetView pickerView:_datePicker withToolbar:_actionToolbar];
  104. }
  105. - (void)pickerDonePressed:(id)sender {
  106. [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
  107. if (_pickerMode == DatePickerModeFull) {
  108. [_dateFormmater setDateFormat:@"yyyy/MM/dd"];
  109. } else if (_pickerMode == DatePickerModeYear) {
  110. [_dateFormmater setDateFormat:@"yyyy"];
  111. }
  112. //request new category.
  113. NSString *stringDate = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:_datePicker.date]];
  114. [self setTitle:stringDate forState:UIControlStateNormal];
  115. if ([self.delegate respondsToSelector:@selector(didDoneClicked:)]) {
  116. [self.delegate didDoneClicked:self];
  117. }
  118. }
  119. - (void)pickerCancelPressed:(id)sender {
  120. [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
  121. }
  122. - (void)setTitle:(NSString *)title forState:(UIControlState)state {
  123. // NSLog(@"%@", [self dateString]);
  124. // NSLog(@"%@", [self formatString]);
  125. if ([title hasPrefix:@"19"] || [title hasPrefix:@"20"]) {
  126. // Date형식 변경에 따라 수정
  127. // [super setTitle:[CommonUtil formattedDate:title] forState:state];
  128. [super setTitle:title forState:state];
  129. if (_datePicker)
  130. {
  131. NSDateComponents *ds = [CommonUtil dateComponentsFromString:title];
  132. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  133. [_datePicker setDate:date animated:NO];
  134. }
  135. } else {
  136. [super setTitle:title forState:state];
  137. }
  138. }
  139. - (void)setDate:(NSDate *)date {
  140. NSString *dateString = [CommonUtil stringFromDate:date];
  141. [self setTitle:dateString forState:UIControlStateNormal];
  142. }
  143. - (NSString *)dateString {
  144. if ([[self titleForState:UIControlStateNormal] isEqualToString:ksEmptyString])
  145. return ksEmptyString;
  146. return _datePicker ? [CommonUtil stringFromDate2:_datePicker.date] : ksEmptyString;
  147. }
  148. - (NSString *)formatString {
  149. return _datePicker ? [CommonUtil stringFromDate:_datePicker.date] : ksEmptyString;
  150. }
  151. - (NSDate *)date {
  152. return _datePicker.date;
  153. }
  154. @end