/
RadioButton.m
141 lines (107 loc) · 3.44 KB
/
RadioButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// RadioButton.m
// RadioButton
//
// Created by ohkawa on 11/03/23.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#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{
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];
}
}
#pragma mark - Manage Instances
+(void)registerInstance:(RadioButton*)radioButton{
if(!rb_instances){
rb_instances = [[NSMutableArray alloc] init];
}
[rb_instances addObject:radioButton];
// Make it weak reference
[radioButton release];
}
#pragma mark - Class level handler
+(void)buttonSelected:(RadioButton*)radioButton{
// Notify observers
if (rb_observers) {
id observer= [rb_observers objectForKey:radioButton.groupId];
if(observer && [observer respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){
[observer radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId];
}
}
// Unselect the other radio buttons
if (rb_instances) {
for (int i = 0; i < [rb_instances count]; i++) {
RadioButton *button = [rb_instances objectAtIndex:i];
if (![button isEqual:radioButton] && [button.groupId isEqualToString:radioButton.groupId]) {
[button otherButtonSelected:radioButton];
}
}
}
}
#pragma mark - Object Lifecycle
-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index{
self = [self init];
if (self) {
_groupId = groupId;
_index = index;
}
return self;
}
- (id)init{
self = [super init];
if (self) {
[self defaultInit];
}
return self;
}
- (void)dealloc
{
[_groupId release];
[_button release];
[super dealloc];
}
#pragma mark - Tap handling
-(void)handleButtonTap:(id)sender{
[_button setSelected:YES];
[RadioButton buttonSelected:self];
}
-(void)otherButtonSelected:(id)sender{
// Called when other radio button instance got selected
if(_button.selected){
[_button setSelected:NO];
}
}
#pragma mark - RadioButton init
-(void)defaultInit{
// Setup container view
self.frame = CGRectMake(0, 0, kRadioButtonWidth, kRadioButtonHeight);
// Customize UIButton
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0,kRadioButtonWidth, kRadioButtonHeight);
_button.adjustsImageWhenHighlighted = NO;
[_button setImage:[UIImage imageNamed:@"RadioButton-Unselected"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"RadioButton-Selected"] forState:UIControlStateSelected];
[_button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
[RadioButton registerInstance:self];
}
@end