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