project_files/HedgewarsMobile/Classes/MasterViewController.m
author koda
Wed, 23 Jun 2010 22:03:56 +0200
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3662 a44406f4369b
permissions -rw-r--r--
i <3 mercurial
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
//  MasterViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 27/03/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 "MasterViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "DetailViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
#import "GeneralSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "TeamSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
#import "WeaponSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
#import "SchemeSettingsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
@implementation MasterViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
@synthesize detailViewController, controllerNames, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    // the list of selectable controllers
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    controllerNames = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
                                                       NSLocalizedString(@"Teams",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
                                                       NSLocalizedString(@"Weapons",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
                                                       NSLocalizedString(@"Schemes",@""),
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
                                                       nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    // the "Done" button on top left
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
                                                                                          target:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
                                                                                          action:@selector(dismissSplitView)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    return [controllerNames count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
        cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    UIViewController *nextController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
        [self.tableView deselectRowAtIndexPath:lastIndexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
        [detailViewController.navigationController popToRootViewControllerAnimated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
        switch (newRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
            case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
                if (nil == generalSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
                    generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
                nextController = generalSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
            case 1:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
                if (nil == teamSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
                    teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
                nextController = teamSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
            case 2:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
                if (nil == weaponSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
                    weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
                nextController = weaponSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
            case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
                if (nil == schemeSettingsViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
                    schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
                nextController = schemeSettingsViewController;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
                break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
        nextController.navigationItem.hidesBackButton = YES;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
        nextController.title = [controllerNames objectAtIndex:newRow];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
        [detailViewController.navigationController pushViewController:nextController animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    // Relinquish ownership any cached data, images, etc that aren't in use.    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    if (generalSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        generalSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    if (teamSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        teamSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    if (weaponSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        weaponSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
    if (schemeSettingsViewController.view.superview == nil)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
        schemeSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
    MSG_MEMCLEAN();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
    self.detailViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    self.controllerNames = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    generalSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    teamSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
    weaponSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
    schemeSettingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    [controllerNames release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    [detailViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
    [generalSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    [teamSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    [weaponSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    [schemeSettingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
-(IBAction) dismissSplitView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155