user3082271 user3082271 - 5 months ago 23
iOS Question

iOS dismissing keyboard, UILabel malfunction

I'm running into a couple problems in the iOS app I'm developing.

One is dismissing the keyboard: while it works on another two screens, there's one screen where it won't dismiss for some reason:

Login.m

#import "Login.h"

@interface Login()
@end

@implementation Login

-(void) viewdDidLoad{

[super viewDidLoad];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];


}

-(void)dismissKeyboard {
[_username resignFirstResponder];
[_password resignFirstResponder];
[_birth resignFirstResponder];
}

@end


And my second problem is that the UILabel at top the screen won't load despite working in the other screens:

Login.m

#import "Login.h"

@interface Login()
@end

@implementation Login

-(void) viewdDidLoad{

[super viewDidLoad];

_etiqueta = @"Introdueix el teu nom d'usuari, la contrasenya, i la teva data de naixement";
self.label.text = self.etiqueta;
self.label.numberOfLines = 4;
self.label.textColor = [UIColor blackColor];

}


@end


My UILabel is created on the storyboard as well, is IBOutlet and I've made sure that it's properly linked.

Can you help me solve these 2 problems?

Answer

use this method of dismiss the keyboard

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}

otherwise call the textDelegate Method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

[textField resignFirstResponder];
return YES;
 }