author | koda |
Fri, 09 Apr 2010 11:09:26 +0000 | |
changeset 3328 | fe87c2242984 |
parent 3325 | 652a8ebdf667 |
child 3330 | 987ec27b6042 |
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 |
||
17 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
|
18 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
19 |
} |
|
3305 | 20 |
|
21 |
#pragma mark - |
|
22 |
#pragma mark View lifecycle |
|
23 |
- (void)viewDidLoad { |
|
24 |
[super viewDidLoad]; |
|
3308 | 25 |
|
3323 | 26 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team navigation") |
27 |
style:UIBarButtonItemStyleBordered |
|
28 |
target:self |
|
29 |
action:@selector(toggleEdit:)]; |
|
30 |
self.navigationItem.rightBarButtonItem = editButton; |
|
31 |
[editButton release]; |
|
32 |
||
3305 | 33 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
3308 | 34 |
NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"/Teams/"]; |
3305 | 35 |
|
3323 | 36 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:teamsDirectory error:NULL]; |
37 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: contentsOfDir copyItems:YES]; |
|
38 |
self.listOfTeams = array; |
|
39 |
[array release]; |
|
40 |
NSLog(@"files: %@", self.listOfTeams); |
|
41 |
} |
|
3308 | 42 |
|
3323 | 43 |
// modifies the navigation bar to add the "Add" and "Done" buttons |
44 |
-(void) toggleEdit:(id) sender { |
|
45 |
BOOL isEditing = self.tableView.editing; |
|
46 |
[self.tableView setEditing:!isEditing animated:YES]; |
|
47 |
||
48 |
if (isEditing) { |
|
49 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Edit",@"from the team navigation")]; |
|
50 |
[self.navigationItem.rightBarButtonItem setStyle: UIBarButtonItemStyleBordered]; |
|
51 |
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem; |
|
52 |
} else { |
|
53 |
[self.navigationItem.rightBarButtonItem setTitle:NSLocalizedString(@"Done",@"from the team navigation")]; |
|
54 |
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone]; |
|
55 |
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add",@"from the team navigation") |
|
56 |
style:UIBarButtonItemStyleBordered |
|
57 |
target:self |
|
58 |
action:@selector(addTeam:)]; |
|
59 |
self.navigationItem.leftBarButtonItem = addButton; |
|
60 |
[addButton release]; |
|
61 |
} |
|
3305 | 62 |
} |
63 |
||
3323 | 64 |
// add a team file with default values and updates the table |
65 |
-(void) addTeam:(id) sender { |
|
3325 | 66 |
NSString *fileName = [[NSString alloc] initWithFormat:@"Default Team %u.plist", [self.listOfTeams count]]; |
3323 | 67 |
|
3325 | 68 |
createTeamNamed([fileName stringByDeletingPathExtension]); |
3323 | 69 |
|
70 |
[self.listOfTeams addObject:fileName]; |
|
71 |
[fileName release]; |
|
72 |
||
73 |
[self.tableView reloadData]; |
|
3305 | 74 |
} |
75 |
||
76 |
#pragma mark - |
|
77 |
#pragma mark Table view data source |
|
3323 | 78 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3305 | 79 |
return 1; |
80 |
} |
|
81 |
||
3323 | 82 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
83 |
return [listOfTeams count]; |
|
3305 | 84 |
} |
85 |
||
86 |
// Customize the appearance of table view cells. |
|
3323 | 87 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 88 |
static NSString *CellIdentifier = @"Cell"; |
89 |
||
90 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
91 |
if (cell == nil) { |
|
92 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
93 |
} |
|
94 |
||
95 |
NSUInteger row = [indexPath row]; |
|
3323 | 96 |
NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
3305 | 97 |
cell.textLabel.text = rowString; |
98 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
99 |
||
100 |
return cell; |
|
101 |
} |
|
102 |
||
3323 | 103 |
// delete the row and the file |
104 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
105 |
NSUInteger row = [indexPath row]; |
|
106 |
||
107 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
|
108 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/Teams/%@",[paths objectAtIndex:0],[self.listOfTeams objectAtIndex:row]]; |
|
3305 | 109 |
|
3323 | 110 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
111 |
[teamFile release]; |
|
112 |
||
113 |
[self.listOfTeams removeObjectAtIndex:row]; |
|
114 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
3305 | 115 |
} |
116 |
||
117 |
/* |
|
118 |
// Override to support editing the table view. |
|
119 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
120 |
||
121 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
122 |
// Delete the row from the data source |
|
123 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
124 |
} |
|
125 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
126 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
127 |
} |
|
128 |
} |
|
129 |
*/ |
|
130 |
||
131 |
/* |
|
132 |
// Override to support rearranging the table view. |
|
133 |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
|
134 |
} |
|
135 |
*/ |
|
136 |
||
137 |
/* |
|
138 |
// Override to support conditional rearranging of the table view. |
|
139 |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
|
140 |
// Return NO if you do not want the item to be re-orderable. |
|
141 |
return YES; |
|
142 |
} |
|
143 |
*/ |
|
144 |
||
145 |
||
146 |
#pragma mark - |
|
147 |
#pragma mark Table view delegate |
|
148 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
|
149 |
if (childController == nil) { |
|
150 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
151 |
} |
|
152 |
||
153 |
NSInteger row = [indexPath row]; |
|
3323 | 154 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
155 |
NSLog(@"%@",selectedTeamFile); |
|
3305 | 156 |
|
3328
fe87c2242984
fix the save of the team and rearrange hat/name modification
koda
parents:
3325
diff
changeset
|
157 |
childController.title = [selectedTeamFile stringByDeletingPathExtension]; |
3325 | 158 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
159 |
||
3323 | 160 |
// this must be set so childController can load the correct plist |
161 |
//childController.title = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
|
3305 | 162 |
[self.navigationController pushViewController:childController animated:YES]; |
163 |
} |
|
164 |
||
165 |
/* |
|
166 |
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { |
|
167 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Hey, do you see the disclosure button?" |
|
168 |
message:@"If you're trying to drill down, touch that instead" |
|
169 |
delegate:nil |
|
170 |
cancelButtonTitle:@"Won't happen again" |
|
171 |
otherButtonTitles:nil]; |
|
172 |
[alert show]; |
|
173 |
[alert release]; |
|
174 |
} |
|
175 |
*/ |
|
176 |
||
177 |
#pragma mark - |
|
178 |
#pragma mark Memory management |
|
3323 | 179 |
-(void) didReceiveMemoryWarning { |
3305 | 180 |
// Releases the view if it doesn't have a superview. |
181 |
[super didReceiveMemoryWarning]; |
|
182 |
||
183 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
184 |
} |
|
185 |
||
3323 | 186 |
-(void) viewDidUnload { |
187 |
self.listOfTeams = nil; |
|
3305 | 188 |
} |
3315 | 189 |
|
3323 | 190 |
-(void) dealloc { |
191 |
[listOfTeams release]; |
|
192 |
[childController release]; |
|
3305 | 193 |
[super dealloc]; |
194 |
} |
|
195 |
||
196 |
||
197 |
@end |
|
198 |