project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
author koda
Tue, 20 Jul 2010 03:14:43 +0200
changeset 3659 f8d5ac50e307
parent 3574 78fc6e61570b
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:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  SettingsViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  hwengine
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 08/01/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "GeneralSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
@implementation GeneralSettingsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
#pragma mark textfield methods
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
// return to previous table
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
-(void) cancel:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    if (textFieldBeingEdited != nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
        [self.textFieldBeingEdited resignFirstResponder];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
// set the new value
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
-(BOOL) save:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    if (textFieldBeingEdited != nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
        if (textFieldBeingEdited.tag == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
            [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
        } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
            [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
        isWriteNeeded = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
        [self.textFieldBeingEdited resignFirstResponder];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
        return YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    return NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
// the textfield is being modified, update the navigation controller
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
-(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    self.textFieldBeingEdited = aTextField;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the settings table")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
                                                                     style:UIBarButtonItemStylePlain
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
                                                                    target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
                                                                    action:@selector(cancel:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    self.navigationItem.leftBarButtonItem = cancelButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [cancelButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the settings table")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
                                                                     style:UIBarButtonItemStyleDone
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
                                                                    target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
                                                                    action:@selector(save:)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    self.navigationItem.rightBarButtonItem = saveButton;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    [saveButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
// the textfield has been modified, check for empty strings and restore original navigation bar
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
-(void) textFieldDidEndEditing:(UITextField *)aTextField{
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    self.textFieldBeingEdited = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
    self.navigationItem.leftBarButtonItem = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
// limit the size of the field to 64 characters like in original frontend
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    int limit = 64;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    return !([textField.text length] > limit && [string length] > range.length);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
#pragma mark View Lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    self.musicSwitch = [[UISwitch alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    self.soundSwitch = [[UISwitch alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    self.altDamageSwitch = [[UISwitch alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    [self.soundSwitch addTarget:self action:@selector(alsoTurnOffMusic:) forControlEvents:UIControlEventValueChanged];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    [self.musicSwitch addTarget:self action:@selector(dontTurnOnMusic:) forControlEvents:UIControlEventValueChanged];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    [self.altDamageSwitch addTarget:self action:@selector(justUpdateDictionary:) forControlEvents:UIControlEventValueChanged];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    self.settingsDictionary = dictionary;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    [dictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
-(void) viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    isWriteNeeded = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
-(void) viewWillDisappear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    [super viewWillDisappear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    if (isWriteNeeded) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
        NSLog(@"writing preferences to file");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
        [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
        isWriteNeeded = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
// if the sound system is off, turn off also the background music 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
-(void) alsoTurnOffMusic:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    if (YES == self.musicSwitch.on) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        [musicSwitch setOn:NO animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    isWriteNeeded = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
// if the sound system is off, don't enable background music 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
-(void) dontTurnOnMusic:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
    if (NO == self.soundSwitch.on) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
        [musicSwitch setOn:NO animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
        [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
        isWriteNeeded = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
-(void) justUpdateDictionary:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    UISwitch *theSwitch = (UISwitch *)sender;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
    isWriteNeeded = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
#pragma mark TableView Methods
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    return 3;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    switch (section) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
        case kNetworkFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
            return 2;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
        case kAudioFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
            return 2;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
        case kOtherFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
            return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
    return 0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    static NSString *cellIdentifier = @"systemSettingsCell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    NSInteger section = [indexPath section];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    if (nil == cell) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
        if (section == kNetworkFields) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-15, 10, 100, 25)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
            label.textAlignment = UITextAlignmentRight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
            label.backgroundColor = [UIColor clearColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
            label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
            if (row == 0) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
                label.text = NSLocalizedString(@"Nickname","from the settings table");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
            else 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
                label.text = NSLocalizedString(@"Password","from the settings table");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
            [cell.contentView addSubview:label];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
            [label release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
            UITextField *aTextField = [[UITextField alloc] initWithFrame:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
                                       CGRectMake(110, 12, (cell.frame.size.width + cell.frame.size.width/3) - 90, 25)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
            aTextField.clearsOnBeginEditing = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
            aTextField.returnKeyType = UIReturnKeyDone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
            aTextField.adjustsFontSizeToFitWidth = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
            aTextField.delegate = self;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   189
            aTextField.tag = row;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
            aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
            [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
            [cell.contentView addSubview:aTextField];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
            [aTextField release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   197
    cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   198
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   199
    cell.imageView.image = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   200
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   201
    UITextField *aTextField;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   202
    switch (section) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   203
        case kNetworkFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   204
            for (UIView *oneView in cell.contentView.subviews) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   205
                if ([oneView isMemberOfClass:[UITextField class]]) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   206
                    aTextField = (UITextField *)oneView;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   207
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   208
            switch (row) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   209
                case 0:                    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   210
                    aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   211
                    aTextField.text = [self.settingsDictionary objectForKey:@"username"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   212
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   213
                case 1:                    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   214
                    aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   215
                    aTextField.text = [self.settingsDictionary objectForKey:@"password"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   216
                    aTextField.secureTextEntry = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   217
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   218
                default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   219
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   220
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   221
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   222
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   223
        case kAudioFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   224
            switch (row) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   225
                case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   226
                    cell.textLabel.text = NSLocalizedString(@"Sound", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   227
                    cell.accessoryView = soundSwitch;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   228
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   229
                case 1:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   230
                    cell.textLabel.text = NSLocalizedString(@"Music", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   231
                    cell.accessoryView = musicSwitch;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   232
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
                default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   234
                    break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   235
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   236
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   237
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   238
        case kOtherFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   239
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   240
            cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   241
            cell.accessoryView = altDamageSwitch;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   242
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   243
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   244
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   245
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   246
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   247
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   248
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   249
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   250
    NSString *sectionTitle = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   251
    switch (section) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   252
        case kNetworkFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   253
            sectionTitle = NSLocalizedString(@"Network Configuration", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   254
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   255
        case kAudioFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   256
            sectionTitle = NSLocalizedString(@"Audio Preferences", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   257
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   258
        case kOtherFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   259
            sectionTitle = NSLocalizedString(@"Other Settings", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   260
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   261
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   262
            NSLog(@"Nope");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   263
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   264
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   265
    return sectionTitle;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   266
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   267
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   268
/*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   269
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   270
    UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   271
    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   272
    headerLabel.textColor = [UIColor lightGrayColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   273
    headerLabel.shadowColor = [UIColor blackColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   274
    headerLabel.shadowOffset = CGSizeMake(0, 1);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   275
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   276
    headerLabel.backgroundColor = [UIColor clearColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   277
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   278
    switch (section) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   279
        case kNetworkFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   280
            headerLabel.text = NSLocalizedString(@"Network Configuration", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   281
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   282
        case kAudioFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   283
            headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   284
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   285
        case kOtherFields:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   286
            headerLabel.text = NSLocalizedString(@"Other Settings", @"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   287
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   288
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   289
            NSLog(@"Warning: unset case value in titleForHeaderInSection!");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   290
            headerLabel.text = @"!";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   291
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   292
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   293
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   294
    [containerView addSubview:headerLabel];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   295
    return containerView;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   296
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   297
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   298
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   299
    if (kAudioFields == [indexPath section] && 2 == [indexPath row])
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   300
        return volumeCell.frame.size.height;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   301
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   302
        return table.rowHeight;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   303
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   304
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   305
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   306
    return 57.0;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   307
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   308
*/
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   309
3574
78fc6e61570b insert a default name when a void string is used
koda
parents: 3547
diff changeset
   310
/*
78fc6e61570b insert a default name when a void string is used
koda
parents: 3547
diff changeset
   311
 causes segfault if pressing twice cancel
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   312
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   313
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   314
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   315
    UITableViewCell *cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   316
    if (kNetworkFields == [indexPath section]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   317
        cell = [aTableView cellForRowAtIndexPath:indexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   318
        for (UIView *oneView in cell.contentView.subviews) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   319
            if ([oneView isMemberOfClass:[UITextField class]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   320
                textFieldBeingEdited = (UITextField *)oneView;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   321
                [textFieldBeingEdited becomeFirstResponder];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   322
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   323
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   324
        [aTableView deselectRowAtIndexPath:indexPath animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   325
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   326
}
3574
78fc6e61570b insert a default name when a void string is used
koda
parents: 3547
diff changeset
   327
*/
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   328
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   329
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   330
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   331
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   332
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   333
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   334
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   335
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   336
    self.settingsDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   337
    self.textFieldBeingEdited = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   338
    self.musicSwitch = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   339
    self.soundSwitch = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   340
    self.altDamageSwitch = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   341
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   342
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   343
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   344
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   345
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   346
    [settingsDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   347
    [textFieldBeingEdited release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   348
    [musicSwitch release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   349
    [soundSwitch release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   350
    [altDamageSwitch release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   351
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   352
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   353
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   354
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   355
@end