diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2065351
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+RadioButton.xcodeproj/project.xcworkspace/xcuserdata
+RadioButton.xcodeproj/xcuserdata
diff --git a/RadioButton.xcodeproj/project.pbxproj b/RadioButton.xcodeproj/project.pbxproj
index bc18c59..914ffaa 100644
--- a/RadioButton.xcodeproj/project.pbxproj
+++ b/RadioButton.xcodeproj/project.pbxproj
@@ -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;
@@ -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;
@@ -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;
diff --git a/RadioButton.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/RadioButton.xcodeproj/project.xcworkspace/contents.xcworkspacedata
new file mode 100644
index 0000000..025e0d4
--- /dev/null
+++ b/RadioButton.xcodeproj/project.xcworkspace/contents.xcworkspacedata
@@ -0,0 +1,7 @@
+
+
+
+
+
diff --git a/RadioButton/RadioButton.h b/RadioButton/RadioButton.h
index 6183079..f4e2336 100644
--- a/RadioButton/RadioButton.h
+++ b/RadioButton/RadioButton.h
@@ -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)delegate;
+
@end
diff --git a/RadioButton/RadioButton.m b/RadioButton/RadioButton.m
old mode 100644
new mode 100755
index 51d640b..1cb4372
--- a/RadioButton/RadioButton.m
+++ b/RadioButton/RadioButton.m
@@ -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)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];
}
}
@@ -47,8 +37,6 @@ +(void)registerInstance:(RadioButton*)radioButton{
}
[rb_instances addObject:radioButton];
- // Make it weak reference
- [radioButton release];
}
#pragma mark - Class level handler
@@ -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{
@@ -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];
diff --git a/RadioButton/RadioButtonViewController.m b/RadioButton/RadioButtonViewController.m
index 866208c..e2f6662 100644
--- a/RadioButton/RadioButtonViewController.m
+++ b/RadioButton/RadioButtonViewController.m
@@ -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];
@@ -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];