author | smaxx |
Wed, 04 Aug 2010 17:35:45 +0200 | |
changeset 3724 | b8678667e7dc |
parent 3697 | d5b30d6373fc |
child 3829 | 81db3c85784b |
permissions | -rw-r--r-- |
3547 | 1 |
// |
2 |
// TeamSettingsViewController.m |
|
3 |
// HedgewarsMobile |
|
4 |
// |
|
5 |
// Created by Vittorio on 02/04/10. |
|
6 |
// Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "TeamSettingsViewController.h" |
|
10 |
#import "SingleTeamViewController.h" |
|
11 |
#import "CommodityFunctions.h" |
|
12 |
||
13 |
@implementation TeamSettingsViewController |
|
14 |
@synthesize listOfTeams; |
|
15 |
||
16 |
||
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
18 |
return rotationManager(interfaceOrientation); |
|
19 |
} |
|
20 |
||
21 |
#pragma mark - |
|
22 |
#pragma mark View lifecycle |
|
23 |
// add an edit button |
|
24 |
-(void) viewDidLoad { |
|
25 |
[super viewDidLoad]; |
|
26 |
||
27 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team panel") |
|
28 |
style:UIBarButtonItemStyleBordered |
|
29 |
target:self |
|
30 |
action:@selector(toggleEdit:)]; |
|
31 |
self.navigationItem.rightBarButtonItem = editButton; |
|
32 |
[editButton release]; |
|
33 |
} |
|
34 |
||
35 |
// load the list of teams in the teams directory |
|
36 |
-(void) viewWillAppear:(BOOL)animated { |
|
37 |
[super viewWillAppear:animated]; |
|
3697 | 38 |
|
3547 | 39 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
40 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
|
41 |
self.listOfTeams = array; |
|
42 |
[array release]; |
|
3697 | 43 |
|
3547 | 44 |
[self.tableView reloadData]; |
45 |
} |
|
46 |
||
47 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
|
48 |
-(void) toggleEdit:(id) sender { |
|
49 |
BOOL isEditing = self.tableView.editing; |
|
50 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
3697 | 51 |
|
3547 | 52 |
if (isEditing) { |
53 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team panel")]; |
|
54 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
55 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
56 |
} else { |
|
57 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team panel")]; |
|
58 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
59 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team panel") |
|
60 |
style:UIBarButtonItemStyleBordered |
|
61 |
target:self |
|
62 |
action:@selector(addTeam:)]; |
|
63 |
self.navigationItem.leftBarButtonItem = addButton; |
|
64 |
[addButton release]; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
// add a team file with default values and updates the table |
|
69 |
-(void) addTeam:(id) sender { |
|
70 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]]; |
|
3697 | 71 |
|
3547 | 72 |
createTeamNamed([fileName stringByDeletingPathExtension]); |
3697 | 73 |
|
3547 | 74 |
[self.listOfTeams addObject:fileName]; |
75 |
[fileName release]; |
|
3697 | 76 |
|
3547 | 77 |
// order the array alphabetically, so teams will keep their position |
78 |
[self.listOfTeams sortUsingSelector:@selector(compare:)]; |
|
3697 | 79 |
|
3547 | 80 |
[self.tableView reloadData]; |
81 |
} |
|
82 |
||
83 |
#pragma mark - |
|
84 |
#pragma mark Table view data source |
|
85 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
|
86 |
return 1; |
|
87 |
} |
|
88 |
||
89 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
|
90 |
return [self.listOfTeams count]; |
|
91 |
} |
|
92 |
||
93 |
// Customize the appearance of table view cells. |
|
94 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
|
95 |
static NSString *CellIdentifier = @"Cell"; |
|
3697 | 96 |
|
3547 | 97 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
98 |
if (cell == nil) { |
|
99 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
100 |
} |
|
3697 | 101 |
|
102 |
NSUInteger row = [indexPath row]; |
|
103 |
NSString *rowString = [[self.listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
|
104 |
cell.textLabel.text = rowString; |
|
3547 | 105 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
3697 | 106 |
|
3547 | 107 |
return cell; |
108 |
} |
|
109 |
||
110 |
// delete the row and the file |
|
111 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
112 |
NSUInteger row = [indexPath row]; |
|
3697 | 113 |
|
3547 | 114 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]]; |
115 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
|
116 |
[teamFile release]; |
|
3697 | 117 |
|
3547 | 118 |
[self.listOfTeams removeObjectAtIndex:row]; |
119 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
120 |
} |
|
121 |
||
122 |
||
123 |
#pragma mark - |
|
124 |
#pragma mark Table view delegate |
|
125 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
126 |
if (childController == nil) { |
|
127 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
128 |
} |
|
3697 | 129 |
|
3547 | 130 |
NSInteger row = [indexPath row]; |
131 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
|
3697 | 132 |
|
3547 | 133 |
// this must be set so childController can load the correct plist |
3660 | 134 |
childController.teamName = [selectedTeamFile stringByDeletingPathExtension]; |
3547 | 135 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
136 |
||
137 |
[self.navigationController pushViewController:childController animated:YES]; |
|
138 |
} |
|
139 |
||
140 |
||
141 |
#pragma mark - |
|
142 |
#pragma mark Memory management |
|
143 |
-(void) didReceiveMemoryWarning { |
|
144 |
// Releases the view if it doesn't have a superview. |
|
145 |
[super didReceiveMemoryWarning]; |
|
146 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
147 |
if (childController.view.superview == nil ) |
|
148 |
childController = nil; |
|
149 |
} |
|
150 |
||
151 |
-(void) viewDidUnload { |
|
152 |
self.listOfTeams = nil; |
|
153 |
childController = nil; |
|
3662
a44406f4369b
polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents:
3660
diff
changeset
|
154 |
MSG_DIDUNLOAD(); |
3547 | 155 |
[super viewDidUnload]; |
156 |
} |
|
157 |
||
158 |
-(void) dealloc { |
|
159 |
[self.listOfTeams release]; |
|
160 |
[childController release]; |
|
161 |
[super dealloc]; |
|
162 |
} |
|
163 |
||
164 |
||
165 |
@end |
|
166 |