Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RadioButton.xcodeproj/project.xcworkspace/xcuserdata
RadioButton.xcodeproj/xcuserdata
5 changes: 5 additions & 0 deletions RadioButton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
/* Begin PBXProject section */
A728E8E7133987E300CF1F36 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0460;
};
buildConfigurationList = A728E8EA133987E300CF1F36 /* Build configuration list for PBXProject "RadioButton" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
Expand Down Expand Up @@ -251,6 +254,7 @@
GCC_DYNAMIC_NO_PIC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "RadioButton/RadioButton-Prefix.pch";
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
INFOPLIST_FILE = "RadioButton/RadioButton-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
Expand All @@ -264,6 +268,7 @@
COPY_PHASE_STRIP = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "RadioButton/RadioButton-Prefix.pch";
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
INFOPLIST_FILE = "RadioButton/RadioButton-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
VALIDATE_PRODUCT = YES;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion RadioButton/RadioButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
@property(nonatomic,assign)NSUInteger index;

-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index;
+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer;

-(void)select;

+(void)setDelegateForGroupId:(NSString*)groupId delegate:(id<RadioButtonDelegate>)delegate;

@end
39 changes: 15 additions & 24 deletions RadioButton/RadioButton.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,24 @@

#import "RadioButton.h"

@interface RadioButton()
-(void)defaultInit;
-(void)otherButtonSelected:(id)sender;
-(void)handleButtonTap:(id)sender;
@end

@implementation RadioButton

@synthesize groupId=_groupId;
@synthesize index=_index;

static const NSUInteger kRadioButtonWidth=22;
static const NSUInteger kRadioButtonHeight=22;

static NSMutableArray *rb_instances=nil;
static NSMutableDictionary *rb_observers=nil;

#pragma mark - Observer

+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer{
+(void)setObserverForGroupId:(NSString*)groupId observer:(id<RadioButtonDelegate>)observer{
if(!rb_observers){
rb_observers = [[NSMutableDictionary alloc] init];
}

if ([groupId length] > 0 && observer) {
[rb_observers setObject:observer forKey:groupId];
// Make it weak reference
[observer release];
}
}

Expand All @@ -47,8 +37,6 @@ +(void)registerInstance:(RadioButton*)radioButton{
}

[rb_instances addObject:radioButton];
// Make it weak reference
[radioButton release];
}

#pragma mark - Class level handler
Expand Down Expand Up @@ -94,19 +82,16 @@ - (id)init{
return self;
}

- (void)dealloc
-(void)select
{
[_groupId release];
[_button release];
[super dealloc];
[_button setSelected:YES];
[RadioButton buttonSelected:self];
}


#pragma mark - Tap handling

-(void)handleButtonTap:(id)sender{
[_button setSelected:YES];
[RadioButton buttonSelected:self];
[self select];
}

-(void)otherButtonSelected:(id)sender{
Expand All @@ -119,16 +104,22 @@ -(void)otherButtonSelected:(id)sender{
#pragma mark - RadioButton init

-(void)defaultInit{
UIImage *unselectedImage = [UIImage imageNamed:@"RadioButton-Unselected"];
UIImage *selectedImage = [UIImage imageNamed:@"RadioButton-Selected"];
CGFloat buttonWidth = unselectedImage.size.width;
CGFloat buttonHeight = unselectedImage.size.height;

// Setup container view
self.frame = CGRectMake(0, 0, kRadioButtonWidth, kRadioButtonHeight);
self.frame = CGRectMake(0, 0, buttonWidth, buttonHeight);

// Customize UIButton
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0,kRadioButtonWidth, kRadioButtonHeight);
_button.frame = CGRectMake(0, 0,buttonWidth, buttonHeight);
_button.adjustsImageWhenHighlighted = NO;

[_button setImage:[UIImage imageNamed:@"RadioButton-Unselected"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"RadioButton-Selected"] forState:UIControlStateSelected];
[_button setImage:unselectedImage forState:UIControlStateNormal];
[_button setImage:selectedImage forState:UIControlStateSelected];
[_button setContentMode:UIViewContentModeScaleAspectFit];

[_button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];

Expand Down
5 changes: 4 additions & 1 deletion RadioButton/RadioButtonViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ - (void)viewDidLoad
questionText.backgroundColor = [UIColor clearColor];
questionText.text = @"Which color do you like?";
[container addSubview:questionText];
[questionText release];

RadioButton *rb1 = [[RadioButton alloc] initWithGroupId:@"first group" index:0];
RadioButton *rb2 = [[RadioButton alloc] initWithGroupId:@"first group" index:1];
Expand Down Expand Up @@ -72,7 +73,9 @@ - (void)viewDidLoad
[container addSubview:label3];
[label3 release];

[RadioButton addObserverForGroupId:@"first group" observer:self];
[rb3 select];

[RadioButton setDelegateForGroupId:@"first group" delegate:self];

[container release];
[super viewDidLoad];
Expand Down