Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // |
2 | // RootViewController.m |
||
3 | // iKopter |
||
4 | // |
||
5 | // Created by Frank Blumenberg on 20.06.10. |
||
6 | // Copyright de.frankblumenberg 2010. All rights reserved. |
||
7 | // |
||
8 | |||
9 | #import "RootViewController.h" |
||
10 | #import "MainViewController.h" |
||
11 | |||
12 | #import "MKHosts.h"; |
||
13 | #import "MKHost.h"; |
||
14 | #import "MKHostViewController.h" |
||
15 | #import "MKConnectionController.h" |
||
16 | |||
17 | @implementation RootViewController |
||
18 | |||
19 | |||
20 | #pragma mark - |
||
21 | #pragma mark View lifecycle |
||
22 | |||
23 | |||
24 | - (void)viewDidLoad { |
||
25 | [super viewDidLoad]; |
||
26 | |||
27 | self.title = @"µKopter"; |
||
28 | |||
29 | hosts=[[MKHosts alloc]init]; |
||
30 | |||
31 | UIBarButtonItem* addButton; |
||
32 | addButton = [[[UIBarButtonItem alloc] |
||
33 | initWithBarButtonSystemItem:UIBarButtonSystemItemAdd |
||
34 | target:self |
||
35 | action:@selector(addHost)] autorelease]; |
||
36 | addButton.style = UIBarButtonItemStyleBordered; |
||
37 | |||
38 | UIBarButtonItem* spacerButton; |
||
39 | spacerButton = [[[UIBarButtonItem alloc] |
||
40 | initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace |
||
41 | target:nil |
||
42 | action:nil] autorelease]; |
||
43 | |||
44 | [self setToolbarItems:[NSArray arrayWithObjects:self.editButtonItem,spacerButton,addButton,nil]]; |
||
45 | |||
46 | self.tableView.allowsSelectionDuringEditing=YES; |
||
47 | } |
||
48 | |||
49 | - (void)viewWillAppear:(BOOL)animated { |
||
50 | [super viewWillAppear:animated]; |
||
51 | |||
52 | self.navigationController.navigationBar.barStyle = UIBarStyleDefault; |
||
53 | [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; |
||
54 | |||
55 | [self.navigationController setToolbarHidden:NO animated:NO]; |
||
56 | [[MKConnectionController sharedMKConnectionController] stop]; |
||
57 | |||
58 | } |
||
59 | |||
60 | - (void)viewDidAppear:(BOOL)animated { |
||
61 | |||
62 | if(self.tableView.editing ) { |
||
63 | |||
64 | if( editingHost ) { |
||
65 | |||
66 | NSArray* indexPaths=[NSArray arrayWithObject:editingHost]; |
||
67 | |||
68 | NSLog(@"appear reload %@",indexPaths); |
||
69 | [self.tableView beginUpdates]; |
||
70 | [self.tableView reloadRowsAtIndexPaths:indexPaths |
||
71 | withRowAnimation:UITableViewRowAnimationFade]; |
||
72 | [self.tableView endUpdates]; |
||
73 | |||
74 | editingHost=nil; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | |||
80 | // Override to allow orientations other than the default portrait orientation. |
||
81 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
||
82 | return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait); |
||
83 | } |
||
84 | |||
85 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
86 | #pragma mark - |
||
87 | #pragma mark Table view data source |
||
88 | |||
89 | // Customize the number of sections in the table view. |
||
90 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
||
91 | return 1; |
||
92 | } |
||
93 | |||
94 | |||
95 | // Customize the number of rows in the table view. |
||
96 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
||
97 | return [hosts count]; |
||
98 | } |
||
99 | |||
100 | |||
101 | // Customize the appearance of table view cells. |
||
102 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
||
103 | |||
104 | NSLog(@"cellForRowAtIndexPath %@",indexPath); |
||
105 | |||
106 | static NSString *CellIdentifier = @"MKHostCell"; |
||
107 | |||
108 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
||
109 | if (cell == nil) { |
||
110 | cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; |
||
111 | } |
||
112 | |||
113 | MKHost* host = [hosts hostAtIndexPath:indexPath]; |
||
114 | |||
115 | cell.textLabel.text = host.name; |
||
116 | cell.detailTextLabel.text = host.address; |
||
117 | cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator; |
||
118 | |||
119 | return cell; |
||
120 | } |
||
121 | |||
122 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
123 | |||
124 | // Override to support conditional editing of the table view. |
||
125 | - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { |
||
126 | // Return NO if you do not want the specified item to be editable. |
||
127 | return YES; |
||
128 | } |
||
129 | |||
130 | |||
131 | // Override to support editing the table view. |
||
132 | - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { |
||
133 | |||
134 | if (editingStyle == UITableViewCellEditingStyleDelete) { |
||
135 | // Delete the row from the data source. |
||
136 | [hosts deleteHostAtIndexPath:indexPath]; |
||
137 | [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; |
||
138 | } |
||
139 | else if (editingStyle == UITableViewCellEditingStyleInsert) { |
||
140 | // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. |
||
141 | } |
||
142 | } |
||
143 | |||
144 | - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { |
||
145 | [hosts moveHostAtIndexPath:fromIndexPath toIndexPath:toIndexPath]; |
||
146 | } |
||
147 | |||
148 | - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { |
||
149 | return YES; |
||
150 | } |
||
151 | |||
152 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
153 | #pragma mark - |
||
154 | #pragma mark Table view delegate |
||
155 | |||
156 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { |
||
157 | |||
158 | [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
||
159 | |||
160 | MKHost* host=[hosts hostAtIndexPath:indexPath]; |
||
161 | if (self.tableView.editing ) { |
||
162 | MKHostViewController* hostView = [[MKHostViewController alloc] initWithHost:host]; |
||
163 | editingHost = indexPath; |
||
164 | [self.navigationController pushViewController:hostView animated:YES]; |
||
165 | [hostView release]; |
||
166 | } |
||
167 | else { |
||
168 | MainViewController* mainView = [[MainViewController alloc] initWithHost:host]; |
||
169 | [self.navigationController pushViewController:mainView animated:YES]; |
||
170 | [mainView release]; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
175 | |||
176 | - (void)addHost { |
||
177 | |||
178 | editingHost=[hosts addHost]; |
||
179 | |||
180 | NSArray* indexPaths=[NSArray arrayWithObject:editingHost]; |
||
181 | |||
182 | [self.tableView beginUpdates]; |
||
183 | [self.tableView insertRowsAtIndexPaths:indexPaths |
||
184 | withRowAnimation:UITableViewRowAnimationFade]; |
||
185 | [self.tableView endUpdates]; |
||
186 | |||
187 | MKHost* host=[hosts hostAtIndexPath:editingHost]; |
||
188 | MKHostViewController* hostView = [[MKHostViewController alloc] initWithHost:host]; |
||
189 | [self.navigationController pushViewController:hostView animated:YES]; |
||
190 | [hostView release]; |
||
191 | } |
||
192 | |||
193 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
||
194 | #pragma mark - |
||
195 | #pragma mark Memory management |
||
196 | |||
197 | - (void)didReceiveMemoryWarning { |
||
198 | // Releases the view if it doesn't have a superview. |
||
199 | [super didReceiveMemoryWarning]; |
||
200 | |||
201 | // Relinquish ownership any cached data, images, etc that aren't in use. |
||
202 | } |
||
203 | |||
204 | - (void)viewDidUnload { |
||
205 | [hosts release]; |
||
206 | hosts = nil; |
||
207 | } |
||
208 | |||
209 | - (void)dealloc { |
||
210 | [hosts release]; |
||
211 | [super dealloc]; |
||
212 | } |
||
213 | |||
214 | |||
215 | @end |
||
216 |