Reputation: 349
I have a login page with username,password textfield and a submit button. If the focus is on password textfield and the user presses the submit button without pressing the return key on keyboard. The keyboard is dismissed as i uses resignFirstResponder to dismiss the keyboard but when i hit the server and the alert comes of invalid credentials,then within nano seconds when alert is shown,keyboard also appears. Used the below line of code to dismiss keyboard on Login Button click.
[self.passwdTxtFld setDelegate:self];
[self.usernameTxtFld setDelegate:self];
[self.view endEditing:YES];
Below is the code from where i get the response from server and also displays the alert.
- (void)checkLogin
{
NSLog(@"???%@",self.usernameTxtFld.text);
NSDictionary *checkLogin=[pwInteract initializeWithOrgId:@"JVVC" AppId:@"JVVC" LoginId:self.usernameTxtFld.text Password:self.passwdTxtFld.text];
NSLog(@"checklogin is %@",checkLogin);
if(checkLogin)
{
if(![[checkLogin objectForKey:@"NOTIFICATION"]isEqualToString:@"Y"] && ![[checkLogin objectForKey:@"NOTIFICATION"]isEqualToString:@"NV"]){
[loggingInAlert dismissWithClickedButtonIndex:loggingInAlert.cancelButtonIndex animated:YES];
[loggingInAlert removeFromSuperview];
NSLog(@"?? response is %@",checkLogin);
NSString *result = [checkLogin objectForKey:@"IS_AUTH"];
NSString *error = [checkLogin objectForKey:@"ERROR"];
if ([result isEqualToString:@"Y"] )
{
if([self.usernameTxtFld.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"username"]]){
[[NSUserDefaults standardUserDefaults] setObject:@"olduser" forKey:@"usertype"];
}
else{
[[NSUserDefaults standardUserDefaults] setObject:@"newuser" forKey:@"usertype"];
}
[[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"fetch"];
[[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"state"];
[[NSUserDefaults standardUserDefaults]setObject:@"success" forKey:@"state"];
[[NSUserDefaults standardUserDefaults]setObject:self.usernameTxtFld.text forKey:@"username"];
[self performSegueWithIdentifier:@"success" sender:nil];
}
else if (![error isEqualToString:@""])
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
networkAlertView.tag = 1;
[networkAlertView show];
[self.view endEditing:YES];
if([self.passwdTxtFld isFirstResponder]){
NSLog(@"pass");
}
if([self.usernameTxtFld isFirstResponder]){
NSLog(@"ddd");
}
}
else if ([result isEqualToString:@""])
{
NSString *error = [checkLogin objectForKey:@"ERROR"];
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
else
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Oops! Either Username or Password is incorrect. Please type correct Username and Password to proceed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
}
else if([[checkLogin objectForKey:@"NOTIFICATION"]isEqualToString:@"NV"]){
[customAlert hideActivityIndicator];
}
}
else
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:@"Server Error" message:@"No Response From Server." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
networkAlertView.tag = 3;
[networkAlertView show];
}
}
On click event of login button have resigned both the username and password textfield,also the UITextFieldDelegate are connected.
Upvotes: 1
Views: 1713
Reputation: 2520
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self view] endEditing:YES];
}
or try
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
// It is important for you to hide the keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Also check UITextFieldDelegate
delegate method
Upvotes: 0
Reputation: 9246
Mistake:-
According to your code you are just resigning your keyboard in one "else if" condition:-
else if (![error isEqualToString:@""])
{
UIAlertView *networkAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
networkAlertView.tag = 1;
[networkAlertView show];
[self.view endEditing:YES];
What about the others else if condition, Things will be resolved if you resign keyboard [self.view endEditing:YES]
in the very first statement of (void)checkLogin
method.
Upvotes: 1
Reputation: 2636
Try [self.view endEditing:YES] when you tap login button or just before you show alert.
Upvotes: 0