project_files/HedgewarsMobile/Classes/TeamSettingsViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11229 b49dfdf628f6
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    22 
    22 
    23 
    23 
    24 @implementation TeamSettingsViewController
    24 @implementation TeamSettingsViewController
    25 @synthesize listOfTeams;
    25 @synthesize listOfTeams;
    26 
    26 
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    29 }
    29 }
    30 
    30 
    31 #pragma mark -
    31 #pragma mark -
    32 #pragma mark View lifecycle
    32 #pragma mark View lifecycle
    33 // add an edit button
    33 // add an edit button
    34 -(void) viewDidLoad {
    34 - (void)viewDidLoad {
    35     [super viewDidLoad];
    35     [super viewDidLoad];
    36 
    36 
    37     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
    37     UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"")
    38                                                                    style:UIBarButtonItemStyleBordered
    38                                                                    style:UIBarButtonItemStyleBordered
    39                                                                   target:self
    39                                                                   target:self
    40                                                                   action:@selector(toggleEdit:)];
    40                                                                   action:@selector(toggleEdit:)];
    41     self.navigationItem.rightBarButtonItem = editButton;
    41     self.navigationItem.rightBarButtonItem = editButton;
    42     [editButton release];
       
    43 
    42 
    44     self.navigationItem.title = NSLocalizedString(@"List of teams", nil);
    43     self.navigationItem.title = NSLocalizedString(@"List of teams", nil);
    45 }
    44 }
    46 
    45 
    47 // load the list of teams in the teams directory
    46 // load the list of teams in the teams directory
    48 -(void) viewWillAppear:(BOOL)animated {
    47 - (void)viewWillAppear:(BOOL)animated {
    49     [super viewWillAppear:animated];
    48     [super viewWillAppear:animated];
    50 
    49 
    51     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    50     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    52     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    51     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    53     self.listOfTeams = array;
    52     self.listOfTeams = array;
    54     [array release];
       
    55 
    53 
    56     [self.tableView reloadData];
    54     [self.tableView reloadData];
    57 }
    55 }
    58 
    56 
    59 // modifies the navigation bar to add the "Add" and "Done" buttons
    57 // modifies the navigation bar to add the "Add" and "Done" buttons
    60 -(void) toggleEdit:(id) sender {
    58 - (void)toggleEdit:(id)sender {
    61     BOOL isEditing = self.tableView.editing;
    59     BOOL isEditing = self.tableView.editing;
    62     [self.tableView setEditing:!isEditing animated:YES];
    60     [self.tableView setEditing:!isEditing animated:YES];
    63 
    61 
    64     if (isEditing) {
    62     if (isEditing) {
    65         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")];
    63         [self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")];
    71         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel")
    69         UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel")
    72                                                                       style:UIBarButtonItemStyleBordered
    70                                                                       style:UIBarButtonItemStyleBordered
    73                                                                      target:self
    71                                                                      target:self
    74                                                                      action:@selector(addTeam:)];
    72                                                                      action:@selector(addTeam:)];
    75         self.navigationItem.leftBarButtonItem = addButton;
    73         self.navigationItem.leftBarButtonItem = addButton;
    76         [addButton release];
       
    77     }
    74     }
    78 }
    75 }
    79 
    76 
    80 // add a team file with default values and updates the table
    77 // add a team file with default values and updates the table
    81 -(void) addTeam:(id) sender {
    78 - (void)addTeam:(id)sender {
    82     NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]];
    79     NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]];
    83 
    80 
    84     [CreationChamber createTeamNamed:[fileName stringByDeletingPathExtension]];
    81     [CreationChamber createTeamNamed:[fileName stringByDeletingPathExtension]];
    85 
    82 
    86     [self.listOfTeams addObject:fileName];
    83     [self.listOfTeams addObject:fileName];
    89     [self.listOfTeams sortUsingSelector:@selector(compare:)];
    86     [self.listOfTeams sortUsingSelector:@selector(compare:)];
    90     [self.tableView reloadData];
    87     [self.tableView reloadData];
    91 
    88 
    92     NSInteger index = [self.listOfTeams indexOfObject:fileName];
    89     NSInteger index = [self.listOfTeams indexOfObject:fileName];
    93     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    90     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    94     [fileName release];
       
    95 }
    91 }
    96 
    92 
    97 #pragma mark -
    93 #pragma mark -
    98 #pragma mark Table view data source
    94 #pragma mark Table view data source
    99 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    95 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   100     return 1;
    96     return 1;
   101 }
    97 }
   102 
    98 
   103 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    99 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   104     return [self.listOfTeams count];
   100     return [self.listOfTeams count];
   105 }
   101 }
   106 
   102 
   107 // Customize the appearance of table view cells.
   103 // Customize the appearance of table view cells.
   108 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   104 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   109     static NSString *CellIdentifier = @"Cell";
   105     static NSString *CellIdentifier = @"Cell";
   110 
   106 
   111     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   107     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   112     if (cell == nil) {
   108     if (cell == nil) {
   113         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   109         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   114     }
   110     }
   115 
   111 
   116     NSUInteger row = [indexPath row];
   112     NSUInteger row = [indexPath row];
   117     NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension];
   113     NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension];
   118     cell.textLabel.text = rowString;
   114     cell.textLabel.text = rowString;
   120 
   116 
   121     return cell;
   117     return cell;
   122 }
   118 }
   123 
   119 
   124 // delete the row and the file
   120 // delete the row and the file
   125 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   121 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   126     NSUInteger row = [indexPath row];
   122     NSUInteger row = [indexPath row];
   127 
   123 
   128     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]];
   124     NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]];
   129     [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
   125     [[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL];
   130     [teamFile release];
       
   131 
   126 
   132     [self.listOfTeams removeObjectAtIndex:row];
   127     [self.listOfTeams removeObjectAtIndex:row];
   133     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   128     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   134 }
   129 }
   135 
   130 
   136 
   131 
   137 #pragma mark -
   132 #pragma mark -
   138 #pragma mark Table view delegate
   133 #pragma mark Table view delegate
   139 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   134 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   140 {
   135 {
   141     SingleTeamViewController *singleTeamViewController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
   136     SingleTeamViewController *singleTeamViewController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped];
   142     
   137     
   143     NSInteger row = [indexPath row];
   138     NSInteger row = [indexPath row];
   144     NSString *selectedTeamFile = [listOfTeams objectAtIndex:row];
   139     NSString *selectedTeamFile = [listOfTeams objectAtIndex:row];
   146     // this must be set so childController can load the correct plist
   141     // this must be set so childController can load the correct plist
   147     singleTeamViewController.teamName = [selectedTeamFile stringByDeletingPathExtension];
   142     singleTeamViewController.teamName = [selectedTeamFile stringByDeletingPathExtension];
   148     [singleTeamViewController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   143     [singleTeamViewController.tableView setContentOffset:CGPointMake(0,0) animated:NO];
   149 
   144 
   150     [self.navigationController pushViewController:singleTeamViewController animated:YES];
   145     [self.navigationController pushViewController:singleTeamViewController animated:YES];
   151     [singleTeamViewController release];
       
   152     
   146     
   153     [tableView deselectRowAtIndexPath:indexPath animated:YES];
   147     [tableView deselectRowAtIndexPath:indexPath animated:YES];
   154 }
   148 }
   155 
   149 
   156 
   150 
   157 #pragma mark -
   151 #pragma mark -
   158 #pragma mark Memory management
   152 #pragma mark Memory management
   159 -(void) didReceiveMemoryWarning
   153 
       
   154 - (void)didReceiveMemoryWarning
   160 {
   155 {
   161     // Releases the view if it doesn't have a superview.
   156     // Releases the view if it doesn't have a superview.
   162     [super didReceiveMemoryWarning];
   157     [super didReceiveMemoryWarning];
   163     // Relinquish ownership any cached data, images, etc that aren't in use.
   158     // Relinquish ownership any cached data, images, etc that aren't in use.
   164 }
   159 }
   165 
   160 
   166 -(void) viewDidUnload
       
   167 {
       
   168     self.listOfTeams = nil;
       
   169     MSG_DIDUNLOAD();
       
   170     [super viewDidUnload];
       
   171 }
       
   172 
       
   173 -(void) dealloc
       
   174 {
       
   175     releaseAndNil(listOfTeams);
       
   176     [super dealloc];
       
   177 }
       
   178 
       
   179 
       
   180 @end
   161 @end
   181 
   162