project_files/HedgewarsMobile/Classes/EditableCellView.m
author koda
Tue, 20 Jul 2010 23:30:54 +0200
changeset 3661 2378ada8a6ee
parent 3660 bc125bea5849
child 3662 a44406f4369b
permissions -rw-r--r--
i can haz panning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     1
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     2
//  WeaponCellView.m
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     3
//  Hedgewars
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     4
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     5
//  Created by Vittorio on 03/07/10.
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     7
//
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     8
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
     9
#import "EditableCellView.h"
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    11
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    12
@implementation EditableCellView
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    13
@synthesize delegate, textField, oldValue;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    14
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    15
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    16
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    17
        delegate = nil;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    18
        
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    19
        textField = [[UITextField alloc] initWithFrame:CGRectZero];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    20
        textField.backgroundColor = [UIColor clearColor];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    21
        textField.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    22
        textField.delegate = self;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    23
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    24
        textField.clearsOnBeginEditing = NO;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    25
        textField.returnKeyType = UIReturnKeyDone;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    26
        textField.adjustsFontSizeToFitWidth = YES;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    27
        [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    28
        
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    29
        [self.contentView addSubview:textField];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    30
        [textField release];
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    31
        
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    32
        oldValue = nil;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    33
    }
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    34
    return self;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    35
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    36
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    37
-(void) layoutSubviews {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    38
    [super layoutSubviews];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    39
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    40
    CGRect contentRect = self.contentView.bounds;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    41
    CGFloat boundsX = contentRect.origin.x;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    42
    
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    43
    int offset = 0;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    44
    if (self.imageView != nil)
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    45
        offset = self.imageView.frame.size.width;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    46
    
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    47
    textField.frame = CGRectMake(boundsX+offset+10, 10, 250, [UIFont labelFontSize] + 4);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    48
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    49
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    50
/*
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    51
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    52
    [super setSelected:selected animated:animated];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    53
    // Configure the view for the selected state
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    54
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    55
*/
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    56
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    57
-(void) dealloc {
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    58
    [oldValue release];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    59
    [textField release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    60
    [super dealloc];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    61
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    62
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    63
#pragma mark -
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    64
#pragma mark textField delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    65
// limit the size of the field to 64 characters like in original frontend
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    66
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    67
    return !([aTextField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    68
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    69
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    70
// allow editing only if delegate is set 
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    71
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    72
    return (delegate != nil);
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    73
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    74
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    75
// the textfield is being modified, update the navigation controller
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    76
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    77
    // don't interact with table below
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    78
    ((UITableView*)[self superview]).scrollEnabled = NO;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    79
    
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    80
    self.oldValue = self.textField.text;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    81
    
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    82
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    83
                                                                     style:UIBarButtonItemStylePlain
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    84
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    85
                                                                    action:@selector(cancel:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    86
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    87
    [cancelButton release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    88
    
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    89
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    90
                                                                     style:UIBarButtonItemStyleDone
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    91
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    92
                                                                    action:@selector(save:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    93
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    94
    [saveButton release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    95
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    96
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    97
/* with this a field might remain in editing status even if the view moved;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
    98
   use method below instead that allows some more interaction
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    99
// don't accept 0-length strings
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   100
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   101
    return ([aTextField.text length] > 0); 
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   102
}
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   103
*/
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   104
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   105
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField {
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   106
    return ([aTextField.text length] > 0); 
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   107
}
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   108
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   109
// the textfield has been modified, tell the delegate to do something
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   110
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   111
    ((UITableView*)[self superview]).scrollEnabled = YES;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   112
    
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   113
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   114
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   115
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   116
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   117
-(void) replyKeyboard {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   118
    [self.textField becomeFirstResponder];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   119
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   120
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   121
// the user pressed cancel so hide keyboard
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   122
-(void) cancel:(id) sender {
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   123
    self.textField.text = self.oldValue;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   124
    [self save:sender];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   125
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   126
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   127
// send the value to the delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   128
-(void) save:(id) sender {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   129
    if (delegate == nil)
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   130
        return;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   131
    
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   132
    // don't save if the textfield is invalid
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   133
    if (![self textFieldShouldReturn:textField])
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   134
        return;
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   135
    
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   136
    [delegate saveTextFieldValue:self.textField.text withTag:self.tag];
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   137
    [self.textField resignFirstResponder];
3660
bc125bea5849 complete settings page rework
koda
parents: 3659
diff changeset
   138
    self.oldValue = nil;
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   139
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   140
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   141
@end