-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberController.m
More file actions
203 lines (157 loc) · 5.63 KB
/
NumberController.m
File metadata and controls
203 lines (157 loc) · 5.63 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// EditStringViewController.m
// BabyNotes
//
// Created by Madson on 10/11/09.
// Copyright 2009 Owera. All rights reserved.
//
#import "NumberController.h"
#import "OWField.h"
@implementation NumberController
@synthesize field;
- (id)initWithDecimalPlaces:(int)aDecimalPlaces {
self = [super initWithNibName:@"NumberController" bundle:nil];
decimalPlaces = aDecimalPlaces;
// Title
self.navigationItem.title = NSLocalizedString(@"Editando", nil);
// Done Button
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Cancelar" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelar)] autorelease];
doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"OK", nil)
style:UIBarButtonItemStyleDone
target:self
action:@selector(doneAction:)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
// Hides Back Button
self.navigationItem.hidesBackButton = YES;
return self;
}
- (void)cancelar {
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)tf {
[textField resignFirstResponder];
[self doneAction:tf];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)tf {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)valueChanged:(id)sender {
// POR ENQUANTO, ESSE METODO NAO ESTA EM UTILIZACAO, POR ISSO O RETURN
return;
//NSLog(@"Antes de processar: %@", textField.text);
NSString *fieldString = [textField.text copy];
NSString *newString = [textField.text copy];
if (decimalPlaces > 0) {
NSString *stringConcatenada;
NSRange range = [fieldString rangeOfString:@","];
if ([fieldString hasPrefix:@","]) {
fieldString = [NSString stringWithFormat:@"0%@", fieldString];
NSString *depoisDaVirgula = [fieldString substringFromIndex:1];
if ([depoisDaVirgula length] < decimalPlaces)
fieldString = [fieldString stringByAppendingString:@"0"];
textField.text = fieldString;
return;
}
while (range.location != NSNotFound) {
NSString *antesDaVirgula = [fieldString substringToIndex:range.location];
NSString *depoisDaVirgula = [fieldString substringFromIndex:range.location+1];
stringConcatenada = [NSString stringWithFormat:@"%@%@", antesDaVirgula, depoisDaVirgula];
newString = [stringConcatenada copy];
range = [newString rangeOfString:@","];
}
NSRange range2;
range2.location = [newString length] - decimalPlaces;
range2.length = decimalPlaces;
NSString *doisUltimosCaracteres = [newString substringFromIndex:range2.location];
NSString *doisUltimosCaracteresComVirgula = [NSString stringWithFormat:@",%@", doisUltimosCaracteres];
newString = [newString stringByReplacingCharactersInRange:range2 withString:doisUltimosCaracteresComVirgula];
if ([newString hasPrefix:@"0"]) {
NSString *newTempString = [newString substringFromIndex:1];
if (decimalPlaces == 0) {
if (([newTempString length] == 0)) {
return;
}
} else {
if (([newTempString length] <= decimalPlaces + 1)) {
return;
}
}
newString = newTempString;
}
//NSLog(@"Nova string (antes de atribuir): %@", newString);
} else {
while ([newString hasPrefix:@"0"]) {
NSString *newTempString = [fieldString substringFromIndex:1];
if ([newTempString length] == 0) {
return;
}
newString = newTempString;
}
}
if (![textField.text isEqualToString:newString])
textField.text = newString;
}
- (void)doneAction:(id)sender {
NSRange range = [textField.text rangeOfString:@","];
NSString *texto = textField.text;
[texto stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (texto == nil || [texto isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Campos nulos", nil)
message:NSLocalizedString(@"Você precisa preencher os campos para continuar.", nil)
delegate:self
cancelButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"OK", nil), nil];
[alert show];
[alert release];
}else{
if (range.location != NSNotFound)
texto = [texto stringByReplacingCharactersInRange:range withString:@"."];
//NSLog(@"Numero Valor: %0.3f", [texto floatValue]);
field.value = [NSNumber numberWithFloat:[texto floatValue]];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellId = @"celulaPersonalizada";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:cellId];
if (nil == cell)
cell = tableViewCell;
return cell;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//textField.text = @"0";
// if (decimalPlaces > 0) {
// while ([textField.text length] <= decimalPlaces + 1) {
// if ([textField.text isEqualToString:@"0"]) {
// textField.text = [textField.text stringByAppendingString:@","];
// } else {
// textField.text = [textField.text stringByAppendingString:@"0"];
// }
// }
// }
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[textField becomeFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.allowsSelection = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
@end