Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LoginTableViewController.m
- // LostAndFound
- //
- // Created by Fabrizio Alongi on 29/06/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "LoginTableViewController.h"
- #import "CreateAccountViewController.h"
- #import "WebServiceWrapper.h"
- #import "MainMenuViewController.h"
- #import "LostViewController.h"
- #import "UserLostViewController.h"
- @implementation LoginTableViewController
- #pragma mark -
- #pragma mark Initialization
- - (id)initWithStyle:(UITableViewStyle)style {
- // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- self = [super initWithStyle:style];
- if (self) {
- // Custom initialization.
- service = [[WebServiceWrapper alloc] init];
- service.delegate = self;
- }
- return self;
- }
- #pragma mark -
- #pragma mark View lifecycle
- - (void)viewDidLoad {
- [super viewDidLoad];
- super.navigationItem.title = @"Lost & Found";
- [self.tableView initWithFrame:CGRectMake(10, 200, 300, 400) style: UITableViewStyleGrouped];
- self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
- self.tableView.scrollEnabled = NO;
- showMenu = NO;
- myUserPass = [[NSMutableArray alloc] init];
- [myUserPass addObject:@"Username"];
- [myUserPass addObject:@"Password"];
- UIBarButtonItem *login = [[[UIBarButtonItem alloc] initWithTitle:@"login" style:UIBarButtonItemStylePlain target:self action:@selector(onLogin:)] autorelease];
- super.navigationItem.rightBarButtonItem = login;
- createAccount = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [createAccount setTitle:@"Crea account" forState:UIControlStateNormal];
- [createAccount setFrame:CGRectMake(10, 300, 300, 30)];
- [createAccount addTarget:self action:@selector(createAccount:) forControlEvents:UIControlEventTouchUpInside];
- [self.tableView addSubview:createAccount];
- self.tableView.backgroundColor = [UIColor clearColor];
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- //self.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- - (void)onLogin:(id)sender {
- [tfPassword resignFirstResponder];
- [tfUsername resignFirstResponder];
- [service loginUserAccount:[NSDictionary dictionaryWithObjectsAndKeys:tfPassword.text,@"Password",tfUsername.text,@"Username",nil]];
- }
- -(void) webServiceRequestFinished:(NSNumber *)responseObject {
- if ([responseObject boolValue]) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[@"Benvenuto " stringByAppendingString:tfUsername.text] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
- [alert show];
- [alert release];
- // carico i dati dell'autenticazione in memoria "permanente"
- NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];
- [UD setObject:tfUsername.text forKey:@"Username"];
- [UD synchronize];
- [self goToMainMenu];
- }
- else {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Utente non valido" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
- }
- // accedo al menù principale
- -(void) goToMainMenu {
- /*
- MainMenuViewController *mainMenu = [[MainMenuViewController alloc] init];
- UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainMenu];
- [self presentModalViewController:navController animated:YES];
- [navController release];
- [mainMenu release];
- */
- //setto la UITabBarController
- UITabBarController *tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
- //all losts
- LostViewController *losts = [[LostViewController alloc] init];
- losts.tabBarItem.title = @"Losts";
- UINavigationController *navControllerLost = [[UINavigationController alloc] initWithRootViewController:losts];
- //lost from user logged
- UserLostViewController *lostsFromUser = [[UserLostViewController alloc] init];
- lostsFromUser.tabBarItem.title = @"My Losts";
- UINavigationController *navControllerLostFromUser = [[UINavigationController alloc] initWithRootViewController:lostsFromUser];
- //main menù
- MainMenuViewController *mainMenu = [[MainMenuViewController alloc] init];
- mainMenu.tabBarItem.title = @"Menù";
- UINavigationController *navControllerMenu = [[UINavigationController alloc] initWithRootViewController:mainMenu];
- //tabBarInit
- tabBar.viewControllers = [NSArray arrayWithObjects:navControllerMenu,navControllerLost,navControllerLostFromUser,nil];
- [self presentModalViewController:tabBar animated:YES];
- //releale all
- }
- - (void)createAccount:(id)sender {
- CreateAccountViewController *newAccountView = [[CreateAccountViewController alloc] init];
- [newAccountView setDelegate:self];
- UINavigationController *tmpNavC = [[UINavigationController alloc] initWithRootViewController:newAccountView];
- [self presentModalViewController:tmpNavC animated:YES];
- [tmpNavC release];
- [newAccountView release];
- }
- -(BOOL) textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return YES;
- }
- -(void)webServiceRequestError:(NSString *)errorName {
- }
- -(void) dismettiCreateAccountController:(NSNumber *)isAuthenticated {
- [self dismissModalViewControllerAnimated:YES];
- if ([isAuthenticated boolValue]) {
- showMenu = YES;
- }
- }
- -(void) viewDidAppear:(BOOL)animated {
- if (showMenu) {
- showMenu = NO;
- [self goToMainMenu];
- }
- }
- #pragma mark -
- #pragma mark Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- // Return the number of sections.
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- // Return the number of rows in the section.
- return [myUserPass count];
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- NSString *cellValue = [myUserPass objectAtIndex:indexPath.row];
- cell.textLabel.text = cellValue;
- // Configure the cell...
- switch (indexPath.row) { //CGRectMake(cell.frame.size.width / 3 + 10, cell.frame.origin.y + 15 , cell.frame.size.width / 2, cell.frame.size.height)
- case 0:
- tfUsername = [[UITextField alloc] initWithFrame: CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 11, 190, 20)];
- tfUsername.delegate = self;
- tfUsername.autocapitalizationType = UITextAutocapitalizationTypeNone;
- tfUsername.textColor = [UIColor blackColor];
- tfUsername.clearButtonMode = UITextFieldViewModeWhileEditing;
- tfUsername.placeholder = @"Obbligatorio";
- [cell.contentView addSubview: tfUsername];
- [tfUsername release];
- break;
- case 1:
- tfPassword = [[UITextField alloc] initWithFrame: CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 11, 190, 20)];
- tfPassword.delegate = self;
- tfPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
- tfPassword.textColor = [UIColor blackColor];
- tfPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
- tfPassword.placeholder = @"Obbligatorio";
- tfPassword.secureTextEntry = YES;
- [cell.contentView addSubview: tfPassword];
- [tfPassword release];
- break;
- default:
- break;
- }
- cell.selectionStyle = UITableViewCellStyleDefault;
- return cell;
- }
- #pragma mark -
- #pragma mark Table view delegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- // Navigation logic may go here. Create and push another view controller.
- /*
- <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
- // ...
- // Pass the selected object to the new view controller.
- [self.navigationController pushViewController:detailViewController animated:YES];
- [detailViewController release];
- */
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Relinquish ownership any cached data, images, etc. that aren't in use.
- }
- - (void)viewDidUnload {
- // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
- // For example: self.myOutlet = nil;
- }
- - (void)dealloc {
- [super dealloc];
- [service release];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement