forked from escoz/QuickDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQBooleanElement.m
138 lines (113 loc) · 4.38 KB
/
QBooleanElement.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
//
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
#import <objc/message.h>
#import "QBooleanElement.h"
#import "QuickDialogController.h"
@implementation QBooleanElement {
}
@synthesize onImage = _onImage;
@synthesize offImage = _offImage;
@synthesize boolValue = _boolValue;
- (QBooleanElement *)init {
self = [self initWithTitle:nil BoolValue:YES];
return self;
}
- (QBooleanElement *)initWithTitle:(NSString *)title BoolValue:(BOOL)value {
self = [self initWithTitle:title Value:nil];
self.boolValue = value;
self.enabled = YES;
return self;
}
-(void)setNumberValue:(NSNumber *)number {
self.boolValue = number.boolValue;
}
-(NSNumber *)numberValue {
return [NSNumber numberWithBool:self.boolValue];
}
- (void)setOnImageName:(NSString *)name {
if(name != nil) {
self.onImage = [UIImage imageNamed:name];
}
}
- (void)setOffImageName:(NSString *)name {
if(name != nil) {
self.offImage = [UIImage imageNamed:name];
}
}
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller {
UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
cell.accessoryType = self.sections!= nil ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
cell.selectionStyle = self.sections!= nil ? UITableViewCellSelectionStyleBlue: UITableViewCellSelectionStyleNone;
if ((_onImage==nil) && (_offImage==nil)) {
UISwitch *boolSwitch = [[UISwitch alloc] init];
boolSwitch.on = self.boolValue;
boolSwitch.enabled = self.enabled;
[boolSwitch addTarget:self action:@selector(switched:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = boolSwitch;
} else {
UIButton *boolButton = [[UIButton alloc] init];
[boolButton setImage:self.offImage forState: UIControlStateNormal];
[boolButton setImage:self.onImage forState: UIControlStateSelected];
[boolButton setImage:self.onImage forState: UIControlStateSelected | UIControlStateDisabled];
cell.accessoryView = boolButton;
boolButton.enabled = self.enabled;
boolButton.selected = self.boolValue;
[boolButton sizeToFit];
[boolButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
- (void)selected:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller indexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.boolValue = !self.boolValue;
if ([cell.accessoryView class] == [UIButton class]) {
((UIButton *)cell.accessoryView).selected = self.boolValue;
}
else if ([cell.accessoryView class] == [UISwitch class]) {
[((UISwitch *)cell.accessoryView) setOn:self.boolValue animated:YES];
}
if (self.controllerAction==nil)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performAction];
}
- (void)buttonPressed:(UIButton *)boolButton {
self.boolValue = !boolButton.selected;
boolButton.selected = _boolValue;
[self performAccessoryAction];
}
-(void)setBoolValue:(BOOL)boolValue {
_boolValue = boolValue;
[self handleEditingChanged];
}
- (void)switched:(id)boolSwitch {
self.boolValue = ((UISwitch *)boolSwitch).on;
if ((self.controller != nil && self.controllerAction != nil) || _onSelected != nil) {
[self performAction];
}
}
- (void)fetchValueIntoObject:(id)obj {
if (_key==nil)
return;
[obj setValue:[NSNumber numberWithBool:self.boolValue] forKey:_key];
}
- (void)setNilValueForKey:(NSString *)key;
{
if ([key isEqualToString:@"boolValue"]){
self.boolValue = NO;
}
else {
[super setNilValueForKey:key];
}
}
@end