RadioButton.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // RadioButton.m
  3. //
  4. // Created by Sergey Nikitenko on 3/5/13.
  5. // Copyright 2013 Sergey Nikitenko. All rights reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "RadioButton.h"
  26. @interface RadioButton()
  27. {
  28. NSMutableArray* _sharedLinks;
  29. }
  30. @end
  31. @implementation RadioButton
  32. - (id)initWithFrame:(CGRect)frame
  33. {
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. if(![[self allTargets] containsObject:self]) {
  37. [super addTarget:self action:@selector(onTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  38. }
  39. }
  40. return self;
  41. }
  42. -(void) awakeFromNib
  43. {
  44. if(![[self allTargets] containsObject:self]) {
  45. [super addTarget:self action:@selector(onTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  46. }
  47. }
  48. -(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
  49. {
  50. // 'self' should be the first target
  51. if(![[self allTargets] containsObject:self]) {
  52. [super addTarget:self action:@selector(onTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
  53. }
  54. [super addTarget:target action:action forControlEvents:controlEvents];
  55. }
  56. -(void) onTouchUpInside
  57. {
  58. [self setSelected:YES distinct:YES sendControlEvent:YES];
  59. }
  60. -(void) setGroupButtons:(NSArray *)buttons
  61. {
  62. if(!_sharedLinks) {
  63. for(RadioButton* rb in buttons) {
  64. if(rb->_sharedLinks) {
  65. _sharedLinks = rb->_sharedLinks;
  66. break;
  67. }
  68. }
  69. if(!_sharedLinks) {
  70. _sharedLinks = [[NSMutableArray alloc] initWithCapacity:[buttons count]+1];
  71. }
  72. }
  73. BOOL (^btnExistsInList)(NSArray*, RadioButton*) = ^(NSArray* list, RadioButton* rb){
  74. for(NSValue* v in list) {
  75. if([v nonretainedObjectValue]==rb) {
  76. return YES;
  77. }
  78. }
  79. return NO;
  80. };
  81. if(!btnExistsInList(_sharedLinks, self)) {
  82. [_sharedLinks addObject:[NSValue valueWithNonretainedObject:self]];
  83. }
  84. for(RadioButton* rb in buttons) {
  85. if(rb->_sharedLinks!=_sharedLinks) {
  86. if(!rb->_sharedLinks) {
  87. rb->_sharedLinks = _sharedLinks;
  88. } else {
  89. for(NSValue* v in rb->_sharedLinks) {
  90. RadioButton* vrb = [v nonretainedObjectValue];
  91. if(!btnExistsInList(_sharedLinks, vrb)) {
  92. [_sharedLinks addObject:v];
  93. vrb->_sharedLinks = _sharedLinks;
  94. }
  95. }
  96. }
  97. }
  98. if(!btnExistsInList(_sharedLinks, rb)) {
  99. [_sharedLinks addObject:[NSValue valueWithNonretainedObject:rb]];
  100. }
  101. }
  102. }
  103. -(NSArray*) groupButtons
  104. {
  105. if([_sharedLinks count]) {
  106. NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:[_sharedLinks count]];
  107. for(NSValue* v in _sharedLinks) {
  108. [buttons addObject:[v nonretainedObjectValue]];
  109. }
  110. return buttons;
  111. }
  112. return nil;
  113. }
  114. -(RadioButton*) selectedButton
  115. {
  116. if([self isSelected]) {
  117. return self;
  118. } else {
  119. for(NSValue* v in _sharedLinks) {
  120. RadioButton* rb = [v nonretainedObjectValue];
  121. if([rb isSelected]) {
  122. return rb;
  123. }
  124. }
  125. }
  126. return nil;
  127. }
  128. -(void) setSelected:(BOOL)selected
  129. {
  130. [self setSelected:selected distinct:YES sendControlEvent:NO];
  131. }
  132. -(void) setButtonSelected:(BOOL)selected sendControlEvent:(BOOL)sendControlEvent
  133. {
  134. BOOL valueChanged = (self.selected != selected);
  135. [super setSelected:selected];
  136. if(valueChanged && sendControlEvent) {
  137. [self sendActionsForControlEvents:UIControlEventValueChanged];
  138. }
  139. }
  140. -(void) setSelected:(BOOL)selected distinct:(BOOL)distinct sendControlEvent:(BOOL)sendControlEvent
  141. {
  142. [self setButtonSelected:selected sendControlEvent:sendControlEvent];
  143. if( distinct && (selected || [_sharedLinks count]==2) )
  144. {
  145. selected = !selected;
  146. for(NSValue* v in _sharedLinks) {
  147. RadioButton* rb = [v nonretainedObjectValue];
  148. if(rb!=self) {
  149. [rb setButtonSelected:selected sendControlEvent:sendControlEvent];
  150. }
  151. }
  152. }
  153. }
  154. -(void) deselectAllButtons
  155. {
  156. for(NSValue* v in _sharedLinks) {
  157. RadioButton* rb = [v nonretainedObjectValue];
  158. [rb setButtonSelected:NO sendControlEvent:NO];
  159. }
  160. }
  161. -(void) setSelectedWithTag:(NSInteger)tag
  162. {
  163. if(self.tag == tag) {
  164. [self setSelected:YES distinct:YES sendControlEvent:NO];
  165. } else {
  166. for(NSValue* v in _sharedLinks) {
  167. RadioButton* rb = [v nonretainedObjectValue];
  168. if(rb.tag == tag) {
  169. [rb setSelected:YES distinct:YES sendControlEvent:NO];
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. - (void)dealloc
  176. {
  177. for(NSValue* v in _sharedLinks) {
  178. if([v nonretainedObjectValue]==self) {
  179. [_sharedLinks removeObjectIdenticalTo:v];
  180. break;
  181. }
  182. }
  183. }
  184. @end