-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBRadioButton.m
70 lines (59 loc) · 1.75 KB
/
BRadioButton.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
//
// BRadioButton.m
// BButtonDemo
//
// Created by Thomas on 15/10/2013.
// Copyright (c) 2013 Hexed Bits. All rights reserved.
//
/**
This is an attempt to replace Segmented Control with some buttons.
Buttons MUST be connected to an action so setSelected is called.
@TODO iOS7 TintColor still interferes with drawing style.
- (IBAction) radioClicked:sender;
{
NSLog(@"BRadioButton %i pressed!",[sender tag]); // assign tag in storyboard
}
*/
#import "BRadioButton.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with automatic reference counting or it will crash your App. Use -fobjc-arc flag on the single file (or convert your project to ARC).
#endif
@implementation BRadioButton
- (void)awakeFromNib
{
[super awakeFromNib];
if(self.selectedColor == nil) // use key value coding in storyboard to set this color!
self.selectedColor = [UIColor darkGrayColor];
}
- (void)setSelected:(BOOL)selected // deselect other buttons
{
if(toNextRadioButton)
{
// loop through buttons until we find self again
BRadioButton* next = toNextRadioButton;
while(next && next != self)
{
if([next isSelected])
[next setSelected:NO];
next = next->toNextRadioButton;
}
}
if(selected && self.selectedColor)
{
self.unselectedColor = self.color;
self.color = self.selectedColor;
}
else
{
if(self.unselectedColor)
self.color = self.unselectedColor;
}
[super setSelected:selected];
[super setHighlighted:selected];
}
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
[super sendAction:action to:target forEvent:event];
[self setSelected:YES];
}
@end