author | koda |
Sat, 10 Apr 2010 01:09:29 +0000 | |
changeset 3330 | 987ec27b6042 |
parent 3328 | fe87c2242984 |
child 3335 | 2520ee7a5484 |
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 |
|
3330 | 23 |
// add an edit button |
24 |
-(void) viewDidLoad { |
|
3305 | 25 |
[super viewDidLoad]; |
3308 | 26 |
|
3323 | 27 |
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit",@"from the team navigation") |
28 |
style:UIBarButtonItemStyleBordered |
|
29 |
target:self |
|
30 |
action:@selector(toggleEdit:)]; |
|
31 |
self.navigationItem.rightBarButtonItem = editButton; |
|
32 |
[editButton release]; |
|
3330 | 33 |
} |
34 |
||
35 |
// load the list of teams in the teams directory |
|
36 |
-(void) viewWillAppear:(BOOL)animated { |
|
37 |
[super viewWillAppear:animated]; |
|
3323 | 38 |
|
3330 | 39 |
NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL]; |
3323 | 40 |
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: contentsOfDir copyItems:YES]; |
41 |
self.listOfTeams = array; |
|
42 |
[array release]; |
|
3330 | 43 |
|
44 |
[self.tableView reloadData]; |
|
3323 | 45 |
NSLog(@"files: %@", self.listOfTeams); |
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 |
||
78 |
[self.tableView reloadData]; |
|
3305 | 79 |
} |
80 |
||
81 |
#pragma mark - |
|
82 |
#pragma mark Table view data source |
|
3323 | 83 |
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { |
3305 | 84 |
return 1; |
85 |
} |
|
86 |
||
3323 | 87 |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
88 |
return [listOfTeams count]; |
|
3305 | 89 |
} |
90 |
||
91 |
// Customize the appearance of table view cells. |
|
3323 | 92 |
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 93 |
static NSString *CellIdentifier = @"Cell"; |
94 |
||
95 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
|
96 |
if (cell == nil) { |
|
97 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
|
98 |
} |
|
99 |
||
100 |
NSUInteger row = [indexPath row]; |
|
3323 | 101 |
NSString *rowString = [[listOfTeams objectAtIndex:row] stringByDeletingPathExtension]; |
3305 | 102 |
cell.textLabel.text = rowString; |
103 |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
|
104 |
||
105 |
return cell; |
|
106 |
} |
|
107 |
||
3323 | 108 |
// delete the row and the file |
109 |
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
110 |
NSUInteger row = [indexPath row]; |
|
111 |
||
3330 | 112 |
NSString *teamFile = [[NSString alloc] initWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[self.listOfTeams objectAtIndex:row]]; |
3323 | 113 |
[[NSFileManager defaultManager] removeItemAtPath:teamFile error:NULL]; |
114 |
[teamFile release]; |
|
115 |
||
116 |
[self.listOfTeams removeObjectAtIndex:row]; |
|
117 |
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
|
3305 | 118 |
} |
119 |
||
120 |
/* |
|
121 |
// Override to support editing the table view. |
|
122 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
|
123 |
||
124 |
if (editingStyle == UITableViewCellEditingStyleDelete) { |
|
125 |
// Delete the row from the data source |
|
126 |
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; |
|
127 |
} |
|
128 |
else if (editingStyle == UITableViewCellEditingStyleInsert) { |
|
129 |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view |
|
130 |
} |
|
131 |
} |
|
132 |
*/ |
|
133 |
||
134 |
||
135 |
#pragma mark - |
|
136 |
#pragma mark Table view delegate |
|
3330 | 137 |
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
3305 | 138 |
if (childController == nil) { |
139 |
childController = [[SingleTeamViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
140 |
} |
|
141 |
||
142 |
NSInteger row = [indexPath row]; |
|
3323 | 143 |
NSString *selectedTeamFile = [listOfTeams objectAtIndex:row]; |
144 |
NSLog(@"%@",selectedTeamFile); |
|
3305 | 145 |
|
3330 | 146 |
// 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
|
147 |
childController.title = [selectedTeamFile stringByDeletingPathExtension]; |
3325 | 148 |
[childController.tableView setContentOffset:CGPointMake(0,0) animated:NO]; |
149 |
||
3305 | 150 |
[self.navigationController pushViewController:childController animated:YES]; |
151 |
} |
|
152 |
||
153 |
||
154 |
#pragma mark - |
|
155 |
#pragma mark Memory management |
|
3323 | 156 |
-(void) didReceiveMemoryWarning { |
3305 | 157 |
// Releases the view if it doesn't have a superview. |
158 |
[super didReceiveMemoryWarning]; |
|
159 |
// Relinquish ownership any cached data, images, etc that aren't in use. |
|
160 |
} |
|
161 |
||
3323 | 162 |
-(void) viewDidUnload { |
163 |
self.listOfTeams = nil; |
|
3330 | 164 |
childController = nil; |
165 |
[super viewDidUnload]; |
|
3305 | 166 |
} |
3315 | 167 |
|
3323 | 168 |
-(void) dealloc { |
169 |
[listOfTeams release]; |
|
170 |
[childController release]; |
|
3305 | 171 |
[super dealloc]; |
172 |
} |
|
173 |
||
174 |
||
175 |
@end |
|
176 |