Pregunta

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?

¿Fue útil?

Solución

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;
 }

Otros consejos

For multiple text fields you can also try something like this. You can keep track of the most active textfield and then resign the responder on it.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeField = textField;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeField = nil;
}

and then dismiss it in your selector

-(void) dismissKeyboard
{
    [self.activeField resignFirstResponder];
}

for the UILabel .. i have tried your code and it works fine..

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top