From e9f245b9599a880793a925c9fc8e6b2cb3cb788b Mon Sep 17 00:00:00 2001 From: Mihai Damian Date: Mon, 4 Mar 2013 14:48:46 +0200 Subject: [PATCH 1/3] Small fixes and improvements --- RadioButton.xcodeproj/project.pbxproj | 5 +++ .../contents.xcworkspacedata | 7 ++++ RadioButton/RadioButton.h | 6 +++- RadioButton/RadioButton.m | 33 ++++++++++--------- RadioButton/RadioButtonViewController.m | 5 ++- 5 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 RadioButton.xcodeproj/project.xcworkspace/contents.xcworkspacedata 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 index 51d640b..ff758a6 100644 --- a/RadioButton/RadioButton.m +++ b/RadioButton/RadioButton.m @@ -23,19 +23,19 @@ @implementation RadioButton static const NSUInteger kRadioButtonHeight=22; static NSMutableArray *rb_instances=nil; -static NSMutableDictionary *rb_observers=nil; +static NSMutableDictionary *rb_delegates=nil; -#pragma mark - Observer +#pragma mark - Delegate -+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer{ - if(!rb_observers){ - rb_observers = [[NSMutableDictionary alloc] init]; ++(void)setDelegateForGroupId:(NSString*)groupId delegate:(id)delegate{ + if(!rb_delegates){ + rb_delegates = [[NSMutableDictionary alloc] init]; } - if ([groupId length] > 0 && observer) { - [rb_observers setObject:observer forKey:groupId]; + if ([groupId length] > 0 && delegate) { + [rb_delegates setObject:delegate forKey:groupId]; // Make it weak reference - [observer release]; + [delegate release]; } } @@ -55,12 +55,12 @@ +(void)registerInstance:(RadioButton*)radioButton{ +(void)buttonSelected:(RadioButton*)radioButton{ - // Notify observers - if (rb_observers) { - id observer= [rb_observers objectForKey:radioButton.groupId]; + // Notify delegates + if (rb_delegates) { + id delegate = [rb_delegates objectForKey:radioButton.groupId]; - if(observer && [observer respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){ - [observer radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId]; + if(delegate && [delegate respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){ + [delegate radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId]; } } @@ -101,12 +101,15 @@ - (void)dealloc [super dealloc]; } +-(void)select{ + [_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{ 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]; From 83889da4f3f1fe80bebdf6c632ef8ecf52fefd5b Mon Sep 17 00:00:00 2001 From: Mihai Damian Date: Mon, 1 Apr 2013 12:18:54 +0300 Subject: [PATCH 2/3] Ignoring Xcode project user settings --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore 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 From 2003540629bfc3583469deeecc78f434d2bcc4c3 Mon Sep 17 00:00:00 2001 From: Mihai Damian Date: Mon, 1 Apr 2013 12:19:17 +0300 Subject: [PATCH 3/3] Removed hardcoded button size --- RadioButton/RadioButton.m | 58 ++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 35 deletions(-) mode change 100644 => 100755 RadioButton/RadioButton.m diff --git a/RadioButton/RadioButton.m b/RadioButton/RadioButton.m old mode 100644 new mode 100755 index ff758a6..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_delegates=nil; +static NSMutableDictionary *rb_observers=nil; -#pragma mark - Delegate +#pragma mark - Observer -+(void)setDelegateForGroupId:(NSString*)groupId delegate:(id)delegate{ - if(!rb_delegates){ - rb_delegates = [[NSMutableDictionary alloc] init]; ++(void)setObserverForGroupId:(NSString*)groupId observer:(id)observer{ + if(!rb_observers){ + rb_observers = [[NSMutableDictionary alloc] init]; } - if ([groupId length] > 0 && delegate) { - [rb_delegates setObject:delegate forKey:groupId]; - // Make it weak reference - [delegate release]; + if ([groupId length] > 0 && observer) { + [rb_observers setObject:observer forKey:groupId]; } } @@ -47,20 +37,18 @@ +(void)registerInstance:(RadioButton*)radioButton{ } [rb_instances addObject:radioButton]; - // Make it weak reference - [radioButton release]; } #pragma mark - Class level handler +(void)buttonSelected:(RadioButton*)radioButton{ - // Notify delegates - if (rb_delegates) { - id delegate = [rb_delegates objectForKey:radioButton.groupId]; + // Notify observers + if (rb_observers) { + id observer= [rb_observers objectForKey:radioButton.groupId]; - if(delegate && [delegate respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){ - [delegate radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId]; + if(observer && [observer respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){ + [observer radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId]; } } @@ -94,14 +82,8 @@ - (id)init{ return self; } -- (void)dealloc +-(void)select { - [_groupId release]; - [_button release]; - [super dealloc]; -} - --(void)select{ [_button setSelected:YES]; [RadioButton buttonSelected:self]; } @@ -122,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];