Skip to content

Commit

Permalink
Add enable multiple selection option;
Browse files Browse the repository at this point in the history
Update test cases.
  • Loading branch information
DavydLiu committed Dec 5, 2015
1 parent 7bcd9d7 commit 543c93b
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 56 deletions.
22 changes: 16 additions & 6 deletions DLRadioButton/DLRadioButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ IB_DESIGNABLE
@property (nonatomic) IBInspectable CGFloat marginWidth;

/**
Whether icon on the right side.
Whether icon on the right side, default is NO.
*/
@property (nonatomic) IBInspectable BOOL isIconOnRight;
@property (nonatomic, getter=isIconOnRight) IBInspectable BOOL iconOnRight;

/**
Whether use square icon.
Whether use square icon, default is NO.
*/
@property (nonatomic) IBInspectable BOOL isIconSquare;
@property (nonatomic, getter=isIconSquare) IBInspectable BOOL iconSquare;

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

/**
@return Current selected button in same group.
Whether enable multiple selection, default is NO.
*/
@property (nonatomic, getter=isMultipleSelectionEnabled) BOOL multipleSelectionEnabled;

/**
@return Selected button in same group.
*/
- (DLRadioButton *)selectedButton;

/**
Clear selection for other buttons in in same group.
@return Selected buttons in same group, use it only if multiple selection is enabled.
*/
- (NSArray *)selectedButtons;

/**
Clears selection for other buttons in in same group.
*/
- (void)deselectOtherButtons;

Expand Down
50 changes: 40 additions & 10 deletions DLRadioButton/DLRadioButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ - (void)setIconSelected:(UIImage *)iconSelected {
[self setImage:self.iconSelected forState:UIControlStateSelected | UIControlStateHighlighted];
}

- (void)setMultipleSelectionEnabled:(BOOL)multipleSelectionEnabled {
if (!self.isChaining) {
self.isChaining = YES;
_multipleSelectionEnabled = multipleSelectionEnabled;
for (DLRadioButton *radioButton in self.otherButtons) {
radioButton.multipleSelectionEnabled = multipleSelectionEnabled;
}
self.isChaining = NO;
}
}

#pragma mark - Helpers

- (void)drawButton {
Expand Down Expand Up @@ -125,16 +136,31 @@ - (void)deselectOtherButtons {
}

- (DLRadioButton *)selectedButton {
if (self.selected) {
return self;
} else {
for (DLRadioButton *radioButton in self.otherButtons) {
if (radioButton.selected) {
return radioButton;
if (!self.isMultipleSelectionEnabled) {
if (self.selected) {
return self;
} else {
for (DLRadioButton *radioButton in self.otherButtons) {
if (radioButton.selected) {
return radioButton;
}
}
}
return nil;
}
return nil;
}

- (NSArray *)selectedButtons {
NSMutableArray *selectedButtons = [[NSMutableArray alloc] init];
if (self.selected) {
[selectedButtons addObject:self];
}
for (DLRadioButton *radioButton in self.otherButtons) {
if (radioButton.selected) {
[selectedButtons addObject:radioButton];
}
}
return selectedButtons;
}

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

- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
[self deselectOtherButtons];
if (self.isMultipleSelectionEnabled) {
[super setSelected:!self.isSelected];
} else {
[super setSelected:selected];
if (selected) {
[self deselectOtherButtons];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = DL;
LastUpgradeCheck = 0510;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = "Xingruo Liu";
TargetAttributes = {
D1BE39FE19A7D263004BD3F5 = {
Expand Down Expand Up @@ -334,6 +334,7 @@
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
Expand Down Expand Up @@ -399,6 +400,7 @@
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
INFOPLIST_FILE = "DLRadioButtonExample/DLRadioButtonExample-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.$(PRODUCT_NAME)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
WRAPPER_EXTENSION = app;
Expand All @@ -416,6 +418,7 @@
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
INFOPLIST_FILE = "DLRadioButtonExample/DLRadioButtonExample-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.$(PRODUCT_NAME)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
WRAPPER_EXTENSION = app;
Expand All @@ -438,6 +441,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "DLRadioButtonExampleTests/DLRadioButtonExampleTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand All @@ -456,6 +460,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "DLRadioButtonExample/DLRadioButtonExample-Prefix.pch";
INFOPLIST_FILE = "DLRadioButtonExampleTests/DLRadioButtonExampleTests-Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.liuxingruo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = xctest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#import <UIKit/UIKit.h>
#import "DLRadioButton.h"

@interface DLDemoViewController : UIViewController

@property (weak, nonatomic) IBOutlet DLRadioButton *waterButton;

@end
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import "DLDemoViewController.h"
#import "DLRadioButton.h"

@implementation DLDemoViewController

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

// enable multiple selection for water, beer and wine buttons.
self.waterButton.multipleSelectionEnabled = YES;

// programmatically add buttons
// first button
DLRadioButton *firstRadioButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(30, 350, self.view.frame.size.width - 60, 15)];
Expand All @@ -40,11 +42,11 @@ - (void)viewDidLoad {
radioButton.iconColor = buttonColor;
radioButton.indicatorColor = buttonColor;
if (i % 2 == 0) {
radioButton.isIconSquare = YES;
radioButton.iconSquare = YES;
}
if (i > 0) {
// put icon on the right side
radioButton.isIconOnRight = YES;
radioButton.iconOnRight = YES;
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
} else {
radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.liuxingruo.$(PRODUCT_NAME)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
Loading

0 comments on commit 543c93b

Please sign in to comment.