RadioButton.m 3.4 KB

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