2

i have two text-Fields one is for Username and second is for mobile-number

username: it is possible for me to hide keyboard for username Text-Fields by using this method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

My problem is mobile-number :

How to hide mobile-number Text-Field?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
PavanAlapati
  • 41
  • 2
  • 8

4 Answers4

5

You can use following library to add done and cancel button on the number pad.

Also You can use tap gesture to hide the number pad.Use following code:

 - (void)viewDidLoad
{

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}

-(void)dismissKeyboard
 {
 [textField resignFirstResponder];//Assuming that UITextField Object is textField
 }

This will work for you..Happy Coding!! Cheers..!!

Raj Shekhar
  • 714
  • 1
  • 4
  • 15
1
 - (void)viewDidLoad
{
    [self addTapGesture];
}

#pragma mark
#pragma mark Gesture  methods

- (void) addTapGesture {
    UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    tapper.cancelsTouchesInView = FALSE;
    [self.view addGestureRecognizer:tapper];
}

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
    [self.view endEditing:YES];
}
Ayaz
  • 1,398
  • 15
  • 29
0

Use

textField.keyboardType = UIKeyboardTypeNumberPad;
Funny
  • 566
  • 5
  • 16
  • 1
    Judging from the given code it's obvious that he wants to hide the keyboard with the return key, but the number pad doesn't have a return key, hence the question. – Desdenova Mar 07 '14 at 11:17
0

There are a couple of options open to you, you need to decide when you want to dismiss your textfields numeric keyboard. For example, if you have a set number of digits you need before you dismiss you could subscribe to

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];

Then in implement something along the lines of

-(void)textFieldTextDidChange:(NSNotification *)notification
{
    UITextField *textfield = notification.object;

    if(textfield.text.length == NUMBER_OF_DIGITS_NEEDED)
        [textField resignFirstResponder];
}

If your user also needs to be able to dismiss the keyboard at their discretion, you should put a tap gesture recognizer on the remaining visible view.

UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGestureRecogniser.delegate = self;
[self.view addGestureRecognizer:tapGestureRecogniser];

-(void)handleTapGesture:(UITapGestureRecognizer *)tapGestureRecogniser
{
    if ([self.myTextField isFirstResponder])
        [self.myTextField resignFirstResponder];
}
user1641587
  • 121
  • 2
  • 2