|
1 // |
|
2 // WeaponCellView.m |
|
3 // Hedgewars |
|
4 // |
|
5 // Created by Vittorio on 03/07/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "EditableCellView.h" |
|
10 #import "CommodityFunctions.h" |
|
11 |
|
12 @implementation EditableCellView |
|
13 @synthesize delegate, textField; |
|
14 |
|
15 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { |
|
16 if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { |
|
17 delegate = nil; |
|
18 |
|
19 textField = [[UITextField alloc] initWithFrame:CGRectZero]; |
|
20 textField.backgroundColor = [UIColor clearColor]; |
|
21 textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; |
|
22 textField.delegate = self; |
|
23 textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
|
24 textField.clearsOnBeginEditing = NO; |
|
25 textField.returnKeyType = UIReturnKeyDone; |
|
26 textField.adjustsFontSizeToFitWidth = YES; |
|
27 [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit]; |
|
28 |
|
29 [self.contentView addSubview:textField]; |
|
30 [textField release]; |
|
31 } |
|
32 return self; |
|
33 } |
|
34 |
|
35 -(void) layoutSubviews { |
|
36 [super layoutSubviews]; |
|
37 |
|
38 CGRect contentRect = self.contentView.bounds; |
|
39 CGFloat boundsX = contentRect.origin.x; |
|
40 |
|
41 textField.frame = CGRectMake(boundsX+10, 11, 250, [UIFont labelFontSize] + 2); |
|
42 } |
|
43 |
|
44 /* |
|
45 -(void) setSelected:(BOOL)selected animated:(BOOL)animated { |
|
46 [super setSelected:selected animated:animated]; |
|
47 // Configure the view for the selected state |
|
48 } |
|
49 */ |
|
50 |
|
51 -(void) dealloc { |
|
52 [textField release]; |
|
53 [super dealloc]; |
|
54 } |
|
55 |
|
56 #pragma mark - |
|
57 #pragma mark textField delegate |
|
58 // limit the size of the field to 64 characters like in original frontend |
|
59 -(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
60 int limit = 64; |
|
61 return !([aTextField.text length] > limit && [string length] > range.length); |
|
62 } |
|
63 |
|
64 // allow editing only if delegate is set |
|
65 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField { |
|
66 return (delegate != nil); |
|
67 } |
|
68 |
|
69 // the textfield is being modified, update the navigation controller |
|
70 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{ |
|
71 // don't scroll when editing |
|
72 ((UITableView*)[self superview]).scrollEnabled = NO; |
|
73 |
|
74 UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"") |
|
75 style:UIBarButtonItemStylePlain |
|
76 target:self |
|
77 action:@selector(cancel:)]; |
|
78 [(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton; |
|
79 [cancelButton release]; |
|
80 |
|
81 UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"") |
|
82 style:UIBarButtonItemStyleDone |
|
83 target:self |
|
84 action:@selector(save:)]; |
|
85 [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton; |
|
86 [saveButton release]; |
|
87 } |
|
88 |
|
89 |
|
90 // don't accept 0-length strings |
|
91 -(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField { |
|
92 return ([aTextField.text length] > 0); |
|
93 } |
|
94 |
|
95 // the textfield has been modified, tell the delegate to do something |
|
96 -(void) textFieldDidEndEditing:(UITextField *)aTextField{ |
|
97 ((UITableView*)[self superview]).scrollEnabled = YES; |
|
98 |
|
99 [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem; |
|
100 [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil; |
|
101 } |
|
102 |
|
103 -(void) replyKeyboard { |
|
104 [self.textField becomeFirstResponder]; |
|
105 } |
|
106 |
|
107 // the user pressed cancel so hide keyboard |
|
108 -(void) cancel:(id) sender { |
|
109 [self.textField resignFirstResponder]; |
|
110 } |
|
111 |
|
112 // send the value to the delegate |
|
113 -(void) save:(id) sender { |
|
114 if (delegate == nil) |
|
115 return; |
|
116 |
|
117 [delegate saveTextFieldValue:self.textField.text]; |
|
118 [self.textField resignFirstResponder]; |
|
119 } |
|
120 |
|
121 @end |