11
11
12
12
@interface BEMCheckBoxGroup ()
13
13
14
- @property (nonatomic , strong , nonnull ) NSArray <BEMCheckBox *> *checkBoxes;
14
+ @property (nonatomic , strong , nonnull ) NSOrderedSet <BEMCheckBox *> *checkBoxes;
15
15
16
16
@end
17
17
@@ -31,7 +31,7 @@ - (instancetype)init {
31
31
self = [super init ];
32
32
if (self) {
33
33
_mustHaveSelection = NO ;
34
- _checkBoxes = @[ ];
34
+ _checkBoxes = [ NSOrderedSet orderedSet ];
35
35
}
36
36
return self;
37
37
}
@@ -46,14 +46,15 @@ + (nonnull instancetype)groupWithCheckBoxes:(nullable NSArray<BEMCheckBox *> *)c
46
46
}
47
47
48
48
- (void )addCheckBoxToGroup : (nonnull BEMCheckBox *)checkBox {
49
- if ([checkBox group ]) {
50
- // Already has a group, remove first
51
- [[checkBox group ] removeCheckBoxFromGroup: checkBox];
49
+ if (checkBox.group ) {
50
+ [checkBox.group removeCheckBoxFromGroup: checkBox];
52
51
}
53
52
54
53
[checkBox _setOn: NO animated: NO notifyGroup: NO ];
55
- [checkBox setGroup: self ];
56
- self.checkBoxes = [self .checkBoxes arrayByAddingObject: checkBox];
54
+ checkBox.group = self;
55
+ NSMutableOrderedSet *mutableBoxes = [self .checkBoxes mutableCopy ];
56
+ [mutableBoxes addObject: checkBox];
57
+ self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet: mutableBoxes];
57
58
}
58
59
59
60
- (void )removeCheckBoxFromGroup : (nonnull BEMCheckBox *)checkBox {
@@ -62,42 +63,46 @@ - (void)removeCheckBoxFromGroup:(nonnull BEMCheckBox *)checkBox {
62
63
return ;
63
64
}
64
65
65
- [ checkBox setGroup: nil ] ;
66
- NSMutableArray *mutableBoxes = [self .checkBoxes mutableCopy ];
66
+ checkBox. group = nil ;
67
+ NSMutableOrderedSet *mutableBoxes = [self .checkBoxes mutableCopy ];
67
68
[mutableBoxes removeObject: checkBox];
68
- self.checkBoxes = [NSArray arrayWithArray : mutableBoxes];
69
+ self.checkBoxes = [NSOrderedSet orderedSetWithOrderedSet : mutableBoxes];
69
70
}
70
71
72
+ #pragma mark Getters
73
+
71
74
- (BEMCheckBox *)selectedCheckBox {
72
- BEMCheckBox *checkbox = nil ;
73
- for (BEMCheckBox *b in self.checkBoxes ) {
74
- if ([b on ] ){
75
- checkbox = b ;
75
+ BEMCheckBox *selected = nil ;
76
+ for (BEMCheckBox *checkBox in self.checkBoxes ) {
77
+ if (checkBox. on ){
78
+ selected = checkBox ;
76
79
break ;
77
80
}
78
81
}
79
82
80
- return checkbox ;
83
+ return selected ;
81
84
}
82
85
86
+ #pragma mark Setters
87
+
83
88
- (void )setSelectedCheckBox : (BEMCheckBox *)selectedCheckBox {
84
89
if (selectedCheckBox) {
85
90
for (BEMCheckBox *checkBox in self.checkBoxes ) {
86
91
BOOL shouldBeOn = (checkBox == selectedCheckBox);
87
- if ([ checkBox on ] != shouldBeOn){
92
+ if (checkBox. on != shouldBeOn){
88
93
[checkBox _setOn: shouldBeOn animated: YES notifyGroup: NO ];
89
94
}
90
95
}
91
96
} else {
92
97
// Selection is nil
93
98
if (self.mustHaveSelection && [self .checkBoxes count ] > 0 ){
94
99
// We must have a selected checkbox, so re-call this method with the first checkbox
95
- [ self setSelectedCheckBox: [self .checkBoxes firstObject ] ];
100
+ self. selectedCheckBox = [self .checkBoxes firstObject ];
96
101
} else {
97
- for (BEMCheckBox *b in self.checkBoxes ) {
102
+ for (BEMCheckBox *checkBox in self.checkBoxes ) {
98
103
BOOL shouldBeOn = NO ;
99
- if ([b on ] != shouldBeOn){
100
- [b _setOn: shouldBeOn animated: YES notifyGroup: NO ];
104
+ if (checkBox. on != shouldBeOn){
105
+ [checkBox _setOn: shouldBeOn animated: YES notifyGroup: NO ];
101
106
}
102
107
}
103
108
}
@@ -109,7 +114,7 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection {
109
114
110
115
// If it must have a selection and we currently don't, select the first box
111
116
if (mustHaveSelection && !self.selectedCheckBox ) {
112
- [ self setSelectedCheckBox: [self .checkBoxes firstObject ] ];
117
+ self. selectedCheckBox = [self .checkBoxes firstObject ];
113
118
}
114
119
}
115
120
@@ -118,10 +123,10 @@ - (void)setMustHaveSelection:(BOOL)mustHaveSelection {
118
123
- (void )_checkBoxSelectionChanged : (BEMCheckBox *)checkBox {
119
124
if ([checkBox on ]) {
120
125
// Change selected checkbox to this one
121
- [ self setSelectedCheckBox: checkBox] ;
126
+ self. selectedCheckBox = checkBox;
122
127
} else if (checkBox == self.selectedCheckBox ) {
123
128
// Selected checkbox was this one, clear it
124
- [ self setSelectedCheckBox: nil ] ;
129
+ self. selectedCheckBox = nil ;
125
130
}
126
131
}
127
132
0 commit comments