0

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?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
user3082271
  • 127
  • 1
  • 1
  • 12

2 Answers2

2

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;
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • The first suggestion did it: guess you need it when you're working with multiple UITextFields. Thank you! – user3082271 May 14 '14 at 10:45
  • what the problem on second means uilabel – Anbu.Karthik May 14 '14 at 10:46
  • yeah, my second problem is that the UILabel won't load the text. It's odd because I've used the same methodology for all of my other screens and it works sans a hitch there. – user3082271 May 14 '14 at 10:47
  • did u tried this _etiqueta = @"Introdueix el teu nom d'usuari, la contrasenya, i la teva data de naixement"; self.label.text=[NSString stringWithFormat:@"%@",_etiqueta]; – Anbu.Karthik May 14 '14 at 10:50
  • I've tried right now but to no avail: the label doesn't change. And the name of the viewDidLoad method is correctly written. – user3082271 May 14 '14 at 10:57
  • k what is this _etiqueta is string or somthing – Anbu.Karthik May 14 '14 at 11:03
  • yeah, it's an NSString. – user3082271 May 14 '14 at 11:05
  • use alternate idea , this is try NSString *str = @"Introdueix el teu nom d'usuari, la contrasenya, i la teva data de naixement"; self.label.text=[NSString stringWithFormat:@"%@", str]; try this – Anbu.Karthik May 14 '14 at 11:08
  • k , NSString *str = @"Introdueix el teu nom d'usuari, la contrasenya, i la teva data de naixement"; print in console NSLog(@"the str==%@",str); try this, if it printed , ur self.label.text is incorrect, k – Anbu.Karthik May 14 '14 at 11:25
0

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..

Prathamesh Saraf
  • 709
  • 4
  • 14