Disable keyboard when clicking on UITextField with custom inputView

I have three UITextFields

UITextField1 <- shows keyboard

UITextField2 <- shows keyboard

UITextField3 <- shows custom inputView (pickerView)

After finishing editing textField2, I press "next" on the keyboard, it hides the keyboard, but the collector is not shown.

If I finish editing textField2 and do not close the keyboard and click on textField3, the keyboard is not hidden and the picker is displayed above the keyboard, then the keyboard cannot be closed unless I go back to textField2 and touch any text field.

snippet:

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


-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField == self.textField3){
        // Close the keyboard.

        [self.textField3 resignFirstResponder];

        [self.view endEditing:YES];

        ActionSheetDatePicker *picker;

        NSString *fechaIni = [self.dates objectAtIndex:0];
        NSString *fechaFin = [self.dates objectAtIndex:[self.dates count]-1];

        NSDate *fechaMarcada;

        if(self.fecha == nil){
            fechaMarcada = [self fromDateString:fechaIni format:@"yyyyMMdd"];
        }else{
            fechaMarcada = self.fecha;
        }

        picker = [[ActionSheetDatePicker alloc] initWithTitle:@"Seleccione" datePickerMode:UIDatePickerModeDate selectedDate:fechaMarcada target:self action:@selector(datePickerValueChanged:) origin:textField cancelAction:nil];

        [picker setMinimumDate:[self fromDateString:fechaIni format:@"yyyyMMdd"]];
        [picker setMaximumDate:[self fromDateString:fechaFin format:@"yyyyMMdd"]];

        [picker setDoneButton:[[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStylePlain target:nil action:nil]];
        [picker setCancelButton:[[UIBarButtonItem alloc] initWithTitle:@"Cancelar" style:UIBarButtonItemStylePlain target:nil action:nil]];
        [picker showActionSheetPicker];

        self.textField3.inputView = picker.pickerView;
        self.textField3.text = [Utils formatDate:[self formatDate:fechaMarcada]];

        [Utils animateScreenUpByHalfKeyboardHeight:self.view];

    }else if(textField == self.textField1){
        if([self.currency.code isEqualToString:@"USD"]){
            textField.keyboardType = UIKeyboardTypeDecimalPad;
        }else{
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }
    }
}

      

How do I close the keyboard in all situations before displaying the pickerView in textField3?

Edit based on rmp suggestion

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [Utils navigationBarTitleAndSubtitle:self title:@"Title" subtitle:@"Subtitle"];

    NSString *fechaIni = [self.dates objectAtIndex:0];
    NSString *fechaFin = [self.dates objectAtIndex:[self.dates count]-1];

    NSDate *fechaMarcada;

    if(self.fecha == nil){
       fechaMarcada = [self fromDateString:fechaIni format:@"yyyyMMdd"];
    }else{
       fechaMarcada = self.fecha;
    }

   self.picker = [[ActionSheetDatePicker alloc] initWithTitle:@"Seleccione" datePickerMode:UIDatePickerModeDate selectedDate:fechaMarcada target:self action:@selector(datePickerValueChanged:) origin:self.textField3 cancelAction:nil];

   [self.picker setMinimumDate:[self fromDateString:fechaIni format:@"yyyyMMdd"]];
   [self.picker setMaximumDate:[self fromDateString:fechaFin format:@"yyyyMMdd"]];

   [self.picker setDoneButton:[[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStylePlain target:nil action:nil]];
   [self.picker setCancelButton:[[UIBarButtonItem alloc] initWithTitle:@"Cancelar" style:UIBarButtonItemStylePlain target:nil action:nil]];
   //[self.picker showActionSheetPicker];

   self.textField3.inputView = self.picker.pickerView;
   self.textField3.text = [Utils formatDate:[self formatDate:fechaMarcada]];
}

-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField == self.textField3){
        // Close the keyboard.
        // [self.view endEditing:YES];

        [Utils animateScreenUpByHalfKeyboardHeight:self.view];

    }else if(textField == self.textField1){
        if([self.currency.code isEqualToString:@"USD"]){
            textField.keyboardType = UIKeyboardTypeDecimalPad;
        }else{
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }
    }
}

      

+3


source to share


4 answers


You don't need to tweak the configuration every time before showing. Move this code to view download to disk or view WillAppear



- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    picker = [[ActionSheetDatePicker alloc] initWithTitle:@"Seleccione" datePickerMode:UIDatePickerModeDate selectedDate:fechaMarcada target:self action:@selector(datePickerValueChanged:) origin:textField cancelAction:nil];

        [picker setMinimumDate:[self fromDateString:fechaIni format:@"yyyyMMdd"]];
        [picker setMaximumDate:[self fromDateString:fechaFin format:@"yyyyMMdd"]];

        [picker setDoneButton:[[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStylePlain target:nil action:nil]];
        [picker setCancelButton:[[UIBarButtonItem alloc] initWithTitle:@"Cancelar" style:UIBarButtonItemStylePlain target:nil action:nil]];
        [picker showActionSheetPicker];

        self.textField3.inputView = picker.pickerView;
}

      

0


source


add a gesture recognizer like this. Make sure to add this UIGestureRecognizerDelegate at the top of your class.



override func viewDidLoad() {
    let tap1 = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    self. UITextField1!.addGestureRecognizer(tap1)
    tap1.delegate = self
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func dismissKeyboard() {
    self.view.endEditing(true)
}

      

0


source


Well it might be related to this piece of code here

self.textField3.inputView = picker.pickerView;
        self.textField3.text = [Utils formatDate:[self formatDate:fechaMarcada]];

        [Utils animateScreenUpByHalfKeyboardHeight:self.view];
      

Run codeHide result


In this case, I think you should remove

[Utils animateScreenUpByHalfKeyboardHeight:self.view];

      

the gesture recognition response is probably right though

0


source


You need to move the ActionSheetDatePicker

init code to viewWillAppear

or viewDidLoad

so that it is initialized before the user can act on the text box. You need this to be textField3.inputView

already assigned before the user enters this field. Now you can remove the line of code [self.textField3 resignFirstResponder];

as your textbox won't display the keyboard as it is inputView

already assignedActionSheetDatePicker

0


source







All Articles