MultiLevelPopupView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // MultiLevelPopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 12/7/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "CustomLabel.h"
  10. #import "CustomButton.h"
  11. #import "MultiLevelPopupView.h"
  12. #import "CustomRadioGroup.h"
  13. #import "CommandClassControlView.h"
  14. @implementation MultiLevelPickerView
  15. - (void)awakeFromNib {
  16. self.layer.backgroundColor = [UIColor clearColor].CGColor;
  17. [self setValue:kUITextColor02 forKey:@"textColor"];
  18. }
  19. - (void)didMoveToSuperview {
  20. // if (!_changed) {
  21. // _changed = YES;
  22. // }
  23. self.showsSelectionIndicator = YES;
  24. self.subviews[1].backgroundColor = kUITextColor02;
  25. self.subviews[2].backgroundColor = kUITextColor02;
  26. }
  27. @end
  28. @interface MultiLevelPopupView () <UIPickerViewDataSource, UIPickerViewDelegate> {
  29. NSMutableArray<ItemModel> *_checkedItems;
  30. NSMutableArray *_cmdclsValueArray;
  31. }
  32. @end
  33. @implementation MultiLevelPopupView
  34. - (id)initFromNib {
  35. for (UIView *view in [CommonUtil nibViews:@"MultiLevelPopupView"]) {
  36. if ([view isKindOfClass:[MultiLevelPopupView class]]) {
  37. self = (MultiLevelPopupView *)view;
  38. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  39. self.frame = [UIScreen mainScreen].bounds;
  40. _rgroup = [[CustomRadioGroup alloc] init];
  41. _rdoUp.value = ksConditionTypeCodeGreatOrEqual;
  42. _rdoDown.value = ksConditionTypeCodeLessOrEqual;
  43. [_rgroup addRadioButton:_rdoUp];
  44. [_rgroup addRadioButton:_rdoDown];
  45. _multiLevelPicker.dataSource = self;
  46. _multiLevelPicker.delegate = self;
  47. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  48. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  49. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  50. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  51. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  52. //Localization
  53. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  54. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  55. }
  56. }
  57. return self;
  58. }
  59. - (void)didMoveToSuperview {
  60. }
  61. #pragma mark - Main Logic
  62. - (void)setRefSubItem:(ItemSubModel *)refSubItem {
  63. _refSubItem = refSubItem;
  64. if ([_refSubItem.conditionTypeCode isEqualToString:ksConditionTypeCodeLessOrEqual]) {
  65. [_rgroup someRadioButtonTouched:_rdoDown];
  66. }
  67. if (!_cmdclsValueArray) {
  68. _cmdclsValueArray = [[NSMutableArray alloc] init];
  69. CGFloat min = [_refSubItem.controlMinValue floatValue];
  70. CGFloat max = [_refSubItem.controlMaxValue floatValue];
  71. CGFloat half = _refSubItem.cmdclsValue && ![_refSubItem.cmdclsValue isEmptyString] ?
  72. [_refSubItem.cmdclsValue floatValue] : (max - min) / 2.0f;
  73. CGFloat i = min;
  74. NSInteger zindex = 0;
  75. BOOL isZeroSet = NO;
  76. while (i <= max) {
  77. if (i != half && !isZeroSet) {
  78. zindex++;
  79. } else if (i == half) {
  80. isZeroSet = YES;
  81. }
  82. //type change --
  83. NSString *cmclsValue = [CommandClassControlView cmdclsValueForType:_refSubItem.dataTypeCode value:i];
  84. [_cmdclsValueArray addObject:cmclsValue];
  85. i += [_refSubItem.controlStep floatValue];
  86. }
  87. [_multiLevelPicker selectRow:zindex inComponent:0 animated:NO];
  88. }
  89. _lblUnit.hidden = !(_refSubItem.unit && ![_refSubItem.unit isEmptyString]);
  90. if (!_lblUnit.hidden) {
  91. _lblUnit.text = _refSubItem.unit;
  92. }
  93. }
  94. - (NSString *)cmdclsValue {
  95. NSString *rvalue = [_cmdclsValueArray objectAtIndex:[_multiLevelPicker selectedRowInComponent:0]];
  96. return rvalue && ![rvalue isEmptyString] ? [CommandClassControlView cmdclsValueForType:_refSubItem.conditionTypeCode value:[rvalue floatValue]]
  97. : ksEmptyString;
  98. }
  99. #pragma mark - Picker Delegate
  100. // 필수 사용메소드 2개 : 이 작업을 하면 피커에 데이터가 들어간다.
  101. // 피커를 사용하기 위해 반드시 사용되어야 할 필수 메소드이다.
  102. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  103. return 1;
  104. }
  105. // 피커를 사용하기 위해 반드시 사용되어야 할 필수 메소드이다.
  106. - (NSInteger)pickerView:(UIPickerView *) pickerView numberOfRowsInComponent : (NSInteger)component{
  107. return _cmdclsValueArray.count;
  108. }
  109. // 피커를 사용하기 위해 반드시 사용되어야 할 필수 델리게이트이다.
  110. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent: (NSInteger)component{
  111. return _cmdclsValueArray[row];
  112. }
  113. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
  114. return 40.0f;
  115. }
  116. #pragma mark - UI Events
  117. - (IBAction)btnConfirmTouched:(id)sender {
  118. //validate
  119. _refSubItem.cmdclsValue = [_cmdclsValueArray objectAtIndex:[_multiLevelPicker selectedRowInComponent:0]];
  120. _refSubItem.conditionTypeCode = _rgroup.valueForChecked;
  121. [super btnConfirmTouched:sender];
  122. }
  123. @end