使用自定义inputView点击UITextField时关闭键盘

时间:2015-04-24 14:26:07

标签: ios objective-c keyboard uitextfield

我有三个UITextFields

UITextField1< - 显示键盘

UITextField2< - 显示键盘

UITextField3< - 显示自定义inputView(pickerView)

完成编辑textField2后,点击" next"在键盘上,它隐藏了键盘,但拾取器未显示。

如果我完成编辑textField2并且没有关闭键盘并点击textField3,键盘就不会被隐藏,而选择器会显示在键盘上方,除非我返回到textField2并点击任何textField外,否则键盘无法关闭。

片段:

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

在textField3中显示pickerView之前,如何在所有情况下关闭键盘?

根据rmp建议进行编辑

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

4 个答案:

答案 0 :(得分:1)

添加这样的手势识别器。确保在您的课程顶部添加此UIGestureRecognizerDelegate。

a=60  (8 bits) -> 00111100
b=113 (8 bits) -> 01110001
c=5   (4 bits) -> 0101

答案 1 :(得分:0)

每次演出前,您都不需要配置选择器。 将此代码移至viewDid加载或viewWillAppear

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

答案 2 :(得分:0)

这可能与此处的代码段有关

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

        [Utils animateScreenUpByHalfKeyboardHeight:self.view];

在这种情况下我认为你应该删除

[Utils animateScreenUpByHalfKeyboardHeight:self.view];

手势识别答案可能是正确的

答案 3 :(得分:0)

您需要将ActionSheetDatePicker初始化代码移至viewWillAppearviewDidLoad,以便在用户对文本字段执行操作之前对其进行初始化。您需要这样,因此在用户输入字段之前已经分配了textField3.inputView。现在,您可以删除代码行[self.textField3 resignFirstResponder];,因为您的文字字段不会显示键盘,因为inputView ActionSheetDatePicker已经分配给--no-package-symlinks