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