project_files/HedgewarsMobile/Classes/EditableCellView.m
author koda
Tue, 20 Jul 2010 03:14:43 +0200
changeset 3659 f8d5ac50e307
child 3660 bc125bea5849
permissions -rw-r--r--
half reworking of the settings page with delegation some graphical enhancements, leaks and fixes
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
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    13
@synthesize delegate, textField;
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];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    31
    }
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    32
    return self;
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
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    35
-(void) layoutSubviews {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    36
    [super layoutSubviews];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    37
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    38
    CGRect contentRect = self.contentView.bounds;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    39
    CGFloat boundsX = contentRect.origin.x;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    40
    
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    41
    textField.frame = CGRectMake(boundsX+10, 11, 250, [UIFont labelFontSize] + 2);
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    42
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    43
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    44
/*
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    45
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    46
    [super setSelected:selected animated:animated];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    47
    // Configure the view for the selected state
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) dealloc {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    52
    [textField release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    53
    [super dealloc];
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
#pragma mark -
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    57
#pragma mark textField delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    58
// 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
    59
-(BOOL) textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    60
    int limit = 64;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    61
    return !([aTextField.text length] > limit && [string length] > range.length);
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
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    64
// allow editing only if delegate is set 
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    65
-(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    66
    return (delegate != nil);
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    67
}
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
// the textfield is being modified, update the navigation controller
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    70
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    71
    // don't scroll when editing
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    72
    ((UITableView*)[self superview]).scrollEnabled = NO;
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
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    75
                                                                     style:UIBarButtonItemStylePlain
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    76
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    77
                                                                    action:@selector(cancel:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    78
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = cancelButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    79
    [cancelButton release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    80
    
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    81
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"")
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    82
                                                                     style:UIBarButtonItemStyleDone
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    83
                                                                    target:self
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    84
                                                                    action:@selector(save:)];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    85
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = saveButton;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    86
    [saveButton release];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    87
}
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
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    90
// don't accept 0-length strings
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    91
-(BOOL) textFieldShouldEndEditing:(UITextField *)aTextField {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    92
    return ([aTextField.text length] > 0); 
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    93
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    94
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    95
// the textfield has been modified, tell the delegate to do something
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    96
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    97
    ((UITableView*)[self superview]).scrollEnabled = YES;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    98
    
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
    99
    [(UITableViewController *)delegate navigationItem].rightBarButtonItem = [(UITableViewController *)delegate navigationItem].backBarButtonItem;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   100
    [(UITableViewController *)delegate navigationItem].leftBarButtonItem = nil;
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   101
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   102
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   103
-(void) replyKeyboard {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   104
    [self.textField becomeFirstResponder];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   105
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   106
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   107
// the user pressed cancel so hide keyboard
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   108
-(void) cancel:(id) sender {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   109
    [self.textField resignFirstResponder];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   110
}
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   111
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   112
// send the value to the delegate
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   113
-(void) save:(id) sender {
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   114
    if (delegate == nil)
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   115
        return;
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
    [delegate saveTextFieldValue:self.textField.text];
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents:
diff changeset
   118
    [self.textField resignFirstResponder];
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
@end