Skip to content

Commit 543c93b

Browse files
committed
Add enable multiple selection option;
Update test cases.
1 parent 7bcd9d7 commit 543c93b

File tree

9 files changed

+140
-56
lines changed

9 files changed

+140
-56
lines changed

DLRadioButton/DLRadioButton.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ IB_DESIGNABLE
4343
@property (nonatomic) IBInspectable CGFloat marginWidth;
4444

4545
/**
46-
Whether icon on the right side.
46+
Whether icon on the right side, default is NO.
4747
*/
48-
@property (nonatomic) IBInspectable BOOL isIconOnRight;
48+
@property (nonatomic, getter=isIconOnRight) IBInspectable BOOL iconOnRight;
4949

5050
/**
51-
Whether use square icon.
51+
Whether use square icon, default is NO.
5252
*/
53-
@property (nonatomic) IBInspectable BOOL isIconSquare;
53+
@property (nonatomic, getter=isIconSquare) IBInspectable BOOL iconSquare;
5454

5555
/**
5656
Image for radio button icon (optional).
@@ -63,12 +63,22 @@ IB_DESIGNABLE
6363
@property (nonatomic) IBInspectable UIImage *iconSelected;
6464

6565
/**
66-
@return Current selected button in same group.
66+
Whether enable multiple selection, default is NO.
67+
*/
68+
@property (nonatomic, getter=isMultipleSelectionEnabled) BOOL multipleSelectionEnabled;
69+
70+
/**
71+
@return Selected button in same group.
6772
*/
6873
- (DLRadioButton *)selectedButton;
6974

7075
/**
71-
Clear selection for other buttons in in same group.
76+
@return Selected buttons in same group, use it only if multiple selection is enabled.
77+
*/
78+
- (NSArray *)selectedButtons;
79+
80+
/**
81+
Clears selection for other buttons in in same group.
7282
*/
7383
- (void)deselectOtherButtons;
7484

DLRadioButton/DLRadioButton.m

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ - (void)setIconSelected:(UIImage *)iconSelected {
3737
[self setImage:self.iconSelected forState:UIControlStateSelected | UIControlStateHighlighted];
3838
}
3939

40+
- (void)setMultipleSelectionEnabled:(BOOL)multipleSelectionEnabled {
41+
if (!self.isChaining) {
42+
self.isChaining = YES;
43+
_multipleSelectionEnabled = multipleSelectionEnabled;
44+
for (DLRadioButton *radioButton in self.otherButtons) {
45+
radioButton.multipleSelectionEnabled = multipleSelectionEnabled;
46+
}
47+
self.isChaining = NO;
48+
}
49+
}
50+
4051
#pragma mark - Helpers
4152

4253
- (void)drawButton {
@@ -125,16 +136,31 @@ - (void)deselectOtherButtons {
125136
}
126137

127138
- (DLRadioButton *)selectedButton {
128-
if (self.selected) {
129-
return self;
130-
} else {
131-
for (DLRadioButton *radioButton in self.otherButtons) {
132-
if (radioButton.selected) {
133-
return radioButton;
139+
if (!self.isMultipleSelectionEnabled) {
140+
if (self.selected) {
141+
return self;
142+
} else {
143+
for (DLRadioButton *radioButton in self.otherButtons) {
144+
if (radioButton.selected) {
145+
return radioButton;
146+
}
134147
}
135148
}
136-
return nil;
137149
}
150+
return nil;
151+
}
152+
153+
- (NSArray *)selectedButtons {
154+
NSMutableArray *selectedButtons = [[NSMutableArray alloc] init];
155+
if (self.selected) {
156+
[selectedButtons addObject:self];
157+
}
158+
for (DLRadioButton *radioButton in self.otherButtons) {
159+
if (radioButton.selected) {
160+
[selectedButtons addObject:radioButton];
161+
}
162+
}
163+
return selectedButtons;
138164
}
139165

140166
#pragma mark - UIButton
@@ -158,9 +184,13 @@ - (UIColor *)titleColorForState:(UIControlState)state {
158184
#pragma mark - UIControl
159185

160186
- (void)setSelected:(BOOL)selected {
161-
[super setSelected:selected];
162-
if (selected) {
163-
[self deselectOtherButtons];
187+
if (self.isMultipleSelectionEnabled) {
188+
[super setSelected:!self.isSelected];
189+
} else {
190+
[super setSelected:selected];
191+
if (selected) {
192+
[self deselectOtherButtons];
193+
}
164194
}
165195
}
166196

DLRadioButtonExample/DLRadioButtonExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
isa = PBXProject;
212212
attributes = {
213213
CLASSPREFIX = DL;
214-
LastUpgradeCheck = 0510;
214+
LastUpgradeCheck = 0700;
215215
ORGANIZATIONNAME = "Xingruo Liu";
216216
TargetAttributes = {
217217
D1BE39FE19A7D263004BD3F5 = {
@@ -334,6 +334,7 @@
334334
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
335335
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
336336
COPY_PHASE_STRIP = NO;
337+
ENABLE_TESTABILITY = YES;
337338
GCC_C_LANGUAGE_STANDARD = gnu99;
338339
GCC_DYNAMIC_NO_PIC = NO;
339340
GCC_OPTIMIZATION_LEVEL = 0;
@@ -399,6 +400,7 @@
399400
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
400401
INFOPLIST_FILE = "DLRadioButtonExample/DLRadioButtonExample-Info.plist";
401402
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
403+
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.$(PRODUCT_NAME)";
402404
PRODUCT_NAME = "$(TARGET_NAME)";
403405
PROVISIONING_PROFILE = "";
404406
WRAPPER_EXTENSION = app;
@@ -416,6 +418,7 @@
416418
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
417419
INFOPLIST_FILE = "DLRadioButtonExample/DLRadioButtonExample-Info.plist";
418420
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
421+
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.$(PRODUCT_NAME)";
419422
PRODUCT_NAME = "$(TARGET_NAME)";
420423
PROVISIONING_PROFILE = "";
421424
WRAPPER_EXTENSION = app;
@@ -438,6 +441,7 @@
438441
"$(inherited)",
439442
);
440443
INFOPLIST_FILE = "DLRadioButtonExampleTests/DLRadioButtonExampleTests-Info.plist";
444+
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.${PRODUCT_NAME:rfc1034identifier}";
441445
PRODUCT_NAME = "$(TARGET_NAME)";
442446
TEST_HOST = "$(BUNDLE_LOADER)";
443447
WRAPPER_EXTENSION = xctest;
@@ -456,6 +460,7 @@
456460
GCC_PRECOMPILE_PREFIX_HEADER = YES;
457461
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
458462
INFOPLIST_FILE = "DLRadioButtonExampleTests/DLRadioButtonExampleTests-Info.plist";
463+
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.${PRODUCT_NAME:rfc1034identifier}";
459464
PRODUCT_NAME = "$(TARGET_NAME)";
460465
TEST_HOST = "$(BUNDLE_LOADER)";
461466
WRAPPER_EXTENSION = xctest;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#import <UIKit/UIKit.h>
2+
#import "DLRadioButton.h"
23

34
@interface DLDemoViewController : UIViewController
45

6+
@property (weak, nonatomic) IBOutlet DLRadioButton *waterButton;
7+
58
@end

DLRadioButtonExample/DLRadioButtonExample/DLDemoViewController.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#import "DLDemoViewController.h"
2-
#import "DLRadioButton.h"
32

43
@implementation DLDemoViewController
54

@@ -14,6 +13,9 @@ - (IBAction)logSelectedButton:(DLRadioButton *)radiobutton {
1413
- (void)viewDidLoad {
1514
[super viewDidLoad];
1615

16+
// enable multiple selection for water, beer and wine buttons.
17+
self.waterButton.multipleSelectionEnabled = YES;
18+
1719
// programmatically add buttons
1820
// first button
1921
DLRadioButton *firstRadioButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(30, 350, self.view.frame.size.width - 60, 15)];
@@ -40,11 +42,11 @@ - (void)viewDidLoad {
4042
radioButton.iconColor = buttonColor;
4143
radioButton.indicatorColor = buttonColor;
4244
if (i % 2 == 0) {
43-
radioButton.isIconSquare = YES;
45+
radioButton.iconSquare = YES;
4446
}
4547
if (i > 0) {
4648
// put icon on the right side
47-
radioButton.isIconOnRight = YES;
49+
radioButton.iconOnRight = YES;
4850
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
4951
} else {
5052
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

DLRadioButtonExample/DLRadioButtonExample/DLRadioButtonExample-Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>CFBundleExecutable</key>
1010
<string>${EXECUTABLE_NAME}</string>
1111
<key>CFBundleIdentifier</key>
12-
<string>com.liuxingruo.$(PRODUCT_NAME)</string>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1313
<key>CFBundleInfoDictionaryVersion</key>
1414
<string>6.0</string>
1515
<key>CFBundleName</key>

0 commit comments

Comments
 (0)