author | sheepluva |
Tue, 04 May 2010 00:44:32 +0000 | |
changeset 3414 | b2f3bb44777e |
parent 3373 | c1ff724a5c34 |
child 3479 | 972ae3ec178a |
permissions | -rw-r--r-- |
3305 | 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" |
|
3325 | 11 |
#import "CommodityFunctions.h" |
3305 | 12 |
|
13 |
@implementation TeamSettingsViewController |
|
3323 | 14 |
@synthesize listOfTeams; |
15 |
||
16 |
||
3335 | 17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
18 |
return rotationManager(interfaceOrientation); |
|
3323 | 19 |
} |
3305 | 20 |
|
3335 | 21 |
|
3305 | 22 |
#pragma mark - |
23 |
#pragma mark View lifecycle |
|
3330 | 24 |
// add an edit button |
25 |
-(void) viewDidLoad { |
|
3305 | 26 |
[super viewDidLoad]; |
3308 | 27 |
|
3323 | 28 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team navigation") |
29 |
style:UIBarButtonItemStyleBordered |
|
30 |
target:self |
|
31 |
action:@selector(toggleEdit:)]; |
|
32 |
self.navigationItem.rightBarButtonItem = editButton; |
|
33 |
[editButton release]; |
|
3330 | 34 |
} |
35 |
||
36 |
// load the list of teams in the teams directory |
|
37 |
-(void) viewWillAppear:(BOOL)animated { |
|
38 |
[super viewWillAppear:animated]; |
|
3323 | 39 |
|
3330 | 40 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
3361 | 41 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES]; |
3323 | 42 |
self.listOfTeams = array; |
43 |
[array release]; |
|
3330 | 44 |
|
45 |
[self.tableView reloadData]; |
|
3323 | 46 |
} |
3308 | 47 |
|
3323 | 48 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
49 |
-(void) toggleEdit:(id) sender { |
|
50 |
BOOL isEditing = self.tableView.editing; |
|
51 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
52 |
||
53 |
if (isEditing) { |
|
54 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team navigation")]; |
|
55 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
56 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
57 |
} else { |
|
58 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team navigation")]; |
|
59 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
60 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team navigation") |
|
61 |
style:UIBarButtonItemStyleBordered |
|
62 |
target:self |
|
63 |
action:@selector(addTeam:)]; |
|
64 |
self.navigationItem.leftBarButtonItem = addButton; |
|
65 |
[addButton release]; |
|
66 |
} |
|
3305 | 67 |
} |
68 |
||
3323 | 69 |
// add a team file with default values and updates the table |
70 |
-(void) addTeam:(id) sender { |
|
3325 | 71 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]]; |
3323 | 72 |
|
3325 | 73 |
createTeamNamed([fileName stringByDeletingPathExtension]); |
3323 | 74 |
|
75 |
[self.listOfTeams addObject:fileName]; |
|
76 |
[fileName release]; |
|
77 |
||
3352 | 78 |
// order the array alphabetically, so teams will keep their position |
79 |
[self.listOfTeams sortUsingSelector:@selector(compare:)]; |
|
80 |
||
3323 | 81 |
[self.tableView reloadData]; |
3305 | 82 |
} |
83 |
||
84 |
#pragma mark - |
|
85 |
#pragma mark Table view data source |
|
3323 | 86 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3305 | 87 |
return 1; |
88 |
} |
|
89 |
||
3323 | 90 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
91 |
return [listOfTeams count]; |
|
3305 | 92 |
} |
93 |
||
94 |
// Customize the appearance of table view cells. |
|
3323 | 95 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 96 |
static NSString *CellIdentifier = @"Cell"; |
97 |
||
98 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
99 |
if (cell == nil) { |
|
100 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
101 |
} |
|
102 |
||
103 |
NSUInteger row = [indexPath row]; |
|
3323 | 104 |
NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
3305 | 105 |
cell.textLabel.text = rowString; |
106 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
107 |
||
108 |
return cell; |
|
109 |
} |
|
110 |
||
3323 | 111 |
// delete the row and the file |
112 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
113 |
NSUInteger row = [indexPath row]; |
|
114 |
||
3330 | 115 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]]; |
3323 | 116 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
117 |
[teamFile release]; |
|
118 |
||
119 |
[self.listOfTeams removeObjectAtIndex:row]; |
|
120 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
3305 | 121 |
} |
122 |
||
123 |
/* |
|
124 |
// Override to support editing the table view. |
|
125 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
126 |
||
127 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
128 |
// Delete the row from the data source |
|
129 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
130 |
} |
|
131 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
132 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
133 |
} |
|
134 |
} |
|
135 |
*/ |
|
136 |
||
137 |
||
138 |
#pragma mark - |
|
139 |
#pragma mark Table view delegate |
|
3330 | 140 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 141 |
if (childController == nil) { |
142 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
143 |
} |
|
144 |
||
145 |
NSInteger row = [indexPath row]; |
|
3323 | 146 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
147 |
NSLog(@"%@",selectedTeamFile); |
|
3305 | 148 |
|
3330 | 149 |
// this must be set so childController can load the correct plist |
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
150 |
childController.title = [selectedTeamFile stringByDeletingPathExtension]; |
3325 | 151 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
152 |
||
3305 | 153 |
[self.navigationController pushViewController:childController animated:YES]; |
154 |
} |
|
155 |
||
156 |
||
157 |
#pragma mark - |
|
158 |
#pragma mark Memory management |
|
3323 | 159 |
-(void) didReceiveMemoryWarning { |
3305 | 160 |
// Releases the view if it doesn't have a superview. |
161 |
[super didReceiveMemoryWarning]; |
|
162 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
163 |
} |
|
164 |
||
3323 | 165 |
-(void) viewDidUnload { |
166 |
self.listOfTeams = nil; |
|
3330 | 167 |
childController = nil; |
168 |
[super viewDidUnload]; |
|
3305 | 169 |
} |
3315 | 170 |
|
3323 | 171 |
-(void) dealloc { |
172 |
[listOfTeams release]; |
|
173 |
[childController release]; |
|
3305 | 174 |
[super dealloc]; |
175 |
} |
|
176 |
||
177 |
||
178 |
@end |
|
179 |