Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. //
  2. // LoginTableViewController.m
  3. // LostAndFound
  4. //
  5. // Created by Fabrizio Alongi on 29/06/11.
  6. // Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "LoginTableViewController.h"
  10. #import "CreateAccountViewController.h"
  11. #import "WebServiceWrapper.h"
  12. #import "MainMenuViewController.h"
  13.  
  14. #import "LostViewController.h"
  15. #import "UserLostViewController.h"
  16.  
  17. @implementation LoginTableViewController
  18.  
  19.  
  20. #pragma mark -
  21. #pragma mark Initialization
  22.  
  23.  
  24. - (id)initWithStyle:(UITableViewStyle)style {
  25. // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  26. self = [super initWithStyle:style];
  27. if (self) {
  28. // Custom initialization.
  29. service = [[WebServiceWrapper alloc] init];
  30. service.delegate = self;
  31. }
  32. return self;
  33. }
  34.  
  35.  
  36.  
  37. #pragma mark -
  38. #pragma mark View lifecycle
  39.  
  40.  
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43.  
  44. super.navigationItem.title = @"Lost & Found";
  45. [self.tableView initWithFrame:CGRectMake(10, 200, 300, 400) style: UITableViewStyleGrouped];
  46. self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
  47. self.tableView.scrollEnabled = NO;
  48.  
  49. showMenu = NO;
  50.  
  51. myUserPass = [[NSMutableArray alloc] init];
  52. [myUserPass addObject:@"Username"];
  53. [myUserPass addObject:@"Password"];
  54.  
  55. UIBarButtonItem *login = [[[UIBarButtonItem alloc] initWithTitle:@"login" style:UIBarButtonItemStylePlain target:self action:@selector(onLogin:)] autorelease];
  56. super.navigationItem.rightBarButtonItem = login;
  57.  
  58. createAccount = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  59. [createAccount setTitle:@"Crea account" forState:UIControlStateNormal];
  60. [createAccount setFrame:CGRectMake(10, 300, 300, 30)];
  61. [createAccount addTarget:self action:@selector(createAccount:) forControlEvents:UIControlEventTouchUpInside];
  62. [self.tableView addSubview:createAccount];
  63.  
  64. self.tableView.backgroundColor = [UIColor clearColor];
  65.  
  66. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  67. //self.navigationItem.rightBarButtonItem = self.editButtonItem;
  68.  
  69. }
  70.  
  71.  
  72. - (void)onLogin:(id)sender {
  73. [tfPassword resignFirstResponder];
  74. [tfUsername resignFirstResponder];
  75.  
  76.  
  77. [service loginUserAccount:[NSDictionary dictionaryWithObjectsAndKeys:tfPassword.text,@"Password",tfUsername.text,@"Username",nil]];
  78.  
  79. }
  80.  
  81.  
  82. -(void) webServiceRequestFinished:(NSNumber *)responseObject {
  83. if ([responseObject boolValue]) {
  84. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[@"Benvenuto " stringByAppendingString:tfUsername.text] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
  85. [alert show];
  86. [alert release];
  87.  
  88. // carico i dati dell'autenticazione in memoria "permanente"
  89. NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
  90. [UD setObject:tfUsername.text forKey:@"Username"];
  91. [UD synchronize];
  92.  
  93. [self goToMainMenu];
  94.  
  95. }
  96. else {
  97. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Utente non valido" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
  98. [alert show];
  99. [alert release];
  100. }
  101. }
  102.  
  103.  
  104. // accedo al menù principale
  105. -(void) goToMainMenu {
  106. /*
  107. MainMenuViewController *mainMenu = [[MainMenuViewController alloc] init];
  108. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainMenu];
  109. [self presentModalViewController:navController animated:YES];
  110. [navController release];
  111. [mainMenu release];
  112. */
  113.  
  114. //setto la UITabBarController
  115. UITabBarController *tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
  116.  
  117. //all losts
  118. LostViewController *losts = [[LostViewController alloc] init];
  119. losts.tabBarItem.title = @"Losts";
  120. UINavigationController *navControllerLost = [[UINavigationController alloc] initWithRootViewController:losts];
  121.  
  122.  
  123. //lost from user logged
  124. UserLostViewController *lostsFromUser = [[UserLostViewController alloc] init];
  125. lostsFromUser.tabBarItem.title = @"My Losts";
  126. UINavigationController *navControllerLostFromUser = [[UINavigationController alloc] initWithRootViewController:lostsFromUser];
  127.  
  128.  
  129. //main menù
  130. MainMenuViewController *mainMenu = [[MainMenuViewController alloc] init];
  131. mainMenu.tabBarItem.title = @"Menù";
  132. UINavigationController *navControllerMenu = [[UINavigationController alloc] initWithRootViewController:mainMenu];
  133.  
  134.  
  135. //tabBarInit
  136. tabBar.viewControllers = [NSArray arrayWithObjects:navControllerMenu,navControllerLost,navControllerLostFromUser,nil];
  137. [self presentModalViewController:tabBar animated:YES];
  138.  
  139. //releale all
  140.  
  141. }
  142.  
  143.  
  144. - (void)createAccount:(id)sender {
  145. CreateAccountViewController *newAccountView = [[CreateAccountViewController alloc] init];
  146. [newAccountView setDelegate:self];
  147. UINavigationController *tmpNavC = [[UINavigationController alloc] initWithRootViewController:newAccountView];
  148. [self presentModalViewController:tmpNavC animated:YES];
  149. [tmpNavC release];
  150. [newAccountView release];
  151. }
  152.  
  153.  
  154. -(BOOL) textFieldShouldReturn:(UITextField *)textField {
  155. [textField resignFirstResponder];
  156. return YES;
  157. }
  158.  
  159.  
  160. -(void)webServiceRequestError:(NSString *)errorName {
  161.  
  162. }
  163.  
  164.  
  165. -(void) dismettiCreateAccountController:(NSNumber *)isAuthenticated {
  166. [self dismissModalViewControllerAnimated:YES];
  167. if ([isAuthenticated boolValue]) {
  168. showMenu = YES;
  169. }
  170. }
  171.  
  172.  
  173. -(void) viewDidAppear:(BOOL)animated {
  174. if (showMenu) {
  175. showMenu = NO;
  176. [self goToMainMenu];
  177. }
  178. }
  179.  
  180.  
  181.  
  182.  
  183. #pragma mark -
  184. #pragma mark Table view data source
  185.  
  186. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  187. // Return the number of sections.
  188. return 1;
  189. }
  190.  
  191.  
  192. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  193. // Return the number of rows in the section.
  194. return [myUserPass count];
  195. }
  196.  
  197.  
  198. // Customize the appearance of table view cells.
  199. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  200.  
  201. static NSString *CellIdentifier = @"Cell";
  202.  
  203. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  204. if (cell == nil) {
  205. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  206. }
  207.  
  208.  
  209. NSString *cellValue = [myUserPass objectAtIndex:indexPath.row];
  210. cell.textLabel.text = cellValue;
  211.  
  212. // Configure the cell...
  213. switch (indexPath.row) { //CGRectMake(cell.frame.size.width / 3 + 10, cell.frame.origin.y + 15 , cell.frame.size.width / 2, cell.frame.size.height)
  214. case 0:
  215. tfUsername = [[UITextField alloc] initWithFrame: CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 11, 190, 20)];
  216. tfUsername.delegate = self;
  217. tfUsername.autocapitalizationType = UITextAutocapitalizationTypeNone;
  218. tfUsername.textColor = [UIColor blackColor];
  219. tfUsername.clearButtonMode = UITextFieldViewModeWhileEditing;
  220. tfUsername.placeholder = @"Obbligatorio";
  221. [cell.contentView addSubview: tfUsername];
  222. [tfUsername release];
  223. break;
  224. case 1:
  225. tfPassword = [[UITextField alloc] initWithFrame: CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 11, 190, 20)];
  226. tfPassword.delegate = self;
  227. tfPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
  228. tfPassword.textColor = [UIColor blackColor];
  229. tfPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
  230. tfPassword.placeholder = @"Obbligatorio";
  231. tfPassword.secureTextEntry = YES;
  232. [cell.contentView addSubview: tfPassword];
  233. [tfPassword release];
  234. break;
  235.  
  236. default:
  237. break;
  238. }
  239.  
  240. cell.selectionStyle = UITableViewCellStyleDefault;
  241. return cell;
  242. }
  243.  
  244.  
  245.  
  246.  
  247. #pragma mark -
  248. #pragma mark Table view delegate
  249.  
  250.  
  251. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  252. // Navigation logic may go here. Create and push another view controller.
  253. /*
  254. <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
  255. // ...
  256. // Pass the selected object to the new view controller.
  257. [self.navigationController pushViewController:detailViewController animated:YES];
  258. [detailViewController release];
  259. */
  260. }
  261.  
  262.  
  263. #pragma mark -
  264. #pragma mark Memory management
  265.  
  266. - (void)didReceiveMemoryWarning {
  267. // Releases the view if it doesn't have a superview.
  268. [super didReceiveMemoryWarning];
  269.  
  270. // Relinquish ownership any cached data, images, etc. that aren't in use.
  271. }
  272.  
  273. - (void)viewDidUnload {
  274. // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
  275. // For example: self.myOutlet = nil;
  276. }
  277.  
  278.  
  279. - (void)dealloc {
  280. [super dealloc];
  281. [service release];
  282. }
  283.  
  284.  
  285.  
  286. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement