1

因为当我单击 UITextField 时日期选择器与键盘一起显示,我想隐藏 dob-text 字段上的键盘操作?这里是我的代码

- (void)removeViews:(id)object 
    {
    [[self.view viewWithTag:9] removeFromSuperview];
    [[self.view viewWithTag:10] removeFromSuperview];
    [[self.view viewWithTag:11] removeFromSuperview];
    }

    - (void)dismissDatePicker:(id)sender 
    {
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
    [UIView beginAnimations:@"MoveOut" context:nil];
    [self.view viewWithTag:9].alpha = 0;
    [self.view viewWithTag:10].frame = datePickerTargetFrame;
    [self.view viewWithTag:11].frame = toolbarTargetFrame;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(removeViews:)];
    [UIView commitAnimations];
    }

    - (IBAction)but 
    {
    //[eventText resignFirstResponder];
    [dob resignFirstResponder];


    if ([self.view viewWithTag:9]) 
    {
        return;
    }
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

    UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ];
    darkView.alpha = 0;
    darkView.backgroundColor = [UIColor blackColor];
    darkView.tag = 9;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] ;
    [darkView addGestureRecognizer:tapGesture];
    [self.view addSubview:darkView];

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
    datePicker.datePickerMode=UIDatePickerModeDate;
    datePicker.tag = 10;
    [datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:datePicker];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ;
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] ;
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:toolBar];

    [UIView beginAnimations:@"MoveIn" context:nil];
    toolBar.frame = toolbarTargetFrame;
    datePicker.frame = datePickerTargetFrame;
    darkView.alpha = 0.5;
    [UIView commitAnimations];
    //[datePicker addTarget:self action:@selector(dateText:)forControlEvents:UIControlEventValueChanged];
    //NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
    //_dateFormatter.dateStyle = NSDateFormatterFullStyle;
    //dateText.text = [NSString stringWithFormat:@"%@",
    // [_dateFormatter stringFromDate:datePicker.date]];

    //[self.tableview reloadData];

    }

I added the resignfirstresponder also,since its showing the same error

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
    return [textField resignFirstResponder];
    return [txt1 resignFirstResponder];//dob textfield
    }

在此处输入图像描述

4

5 回答 5

6

使用 UITextField 的委托方法

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
 {
   if(textField == dob-text )
     //load picker here
     return NO; //hide keyboard
   else
     return YES; //show keyboard
 }
于 2012-09-21T09:40:29.360 回答
3

更好的解决方案是将 UIPickerView 设置inputViewUITextField.

您可以将 设置UIToolBarinputAccessoryViewinputView。这样 iOS 将处理所有 UIPickView 的显示。

只需将属性设置为正确的视图:

self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.inputToolbar;

另一方面,使用viewWithTag:方法获取视图意味着您正在遍历所有视图。为什么不为这些视图创建属性。它会不那么混乱,可能会更快。

于 2012-09-21T09:50:33.380 回答
0

首先,文本字段不应该有弹出框 - 既不是选择器也不是弹出框,也不是任何东西最好的解决方案是放置一个按钮 - 输入自定义 - 设置图像,如日期选择器图像并添加选择器 - 在这种情况下,您将没有任何问题 wrt textfields popover 并且外观也会很好,您可以根据您的要求对其进行自定义。

于 2012-09-22T05:03:45.793 回答
0

试试 textFieldDidBeginEditing

-(void) textFieldDidBeginEditing:(UITextField *)textField{
  [textField resignFirstResponder];
}
于 2012-09-21T09:45:18.923 回答
0
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    datePicker.hidden = NO;
    [textField resignFirstResponder];
}
于 2012-09-21T09:48:36.910 回答