当选择器显示时,辞职第一响应者无法工作

时间:2015-07-16 13:19:20

标签: ios

我遇到了日期选择器的问题。当我触摸textfield(datumletu)时,我调用picker和datumletu [resign firstresponder]。但没有效果。 这是我的代码。我尝试了一切,但没有任何工作。

-(void)textFieldDidBeginEditing:(UITextField *)textField {    
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

if (textField == self.datumLetu) {
    [self.view endEditing:YES];
  [self.autoTextFieldPriletu resignFirstResponder];
  [self.autoTextField resignFirstResponder];
  [self.cisloLetu resignFirstResponder];

    [self.datumLetu resignFirstResponder];
    self.picker.hidden = YES;
    [self showPicker];

    CGRect rect = self.view.frame;

    if(rect.origin.y == 0)
    {
        rect.origin.y -= 110;
        rect.size.height += 110;
        [UIView animateWithDuration:0.2 animations:^{
            self.view.frame = rect;
        }];
        self.picker.frame = CGRectMake(0, screenHeight / 2 +  screenHeight / 3.2, screenWidth, screenHeight/2);

    }

和选择器

-(void)showPicker
{
    [self showAction];
}
 -(void) showAction
 {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
    self.picker = [[Picker alloc] initWithFrame:CGRectMake(0, screenHeight / 2 +  screenHeight / 10.5, screenWidth, screenHeight/2)];
[self.picker addTargetForDoneButton:self action:@selector(cancelPressed)];
[self.picker addTargetForCancelButton:self action:@selector(cancelPressed)];

[self.picker setMode:UIDatePickerModeDateAndTime];
[self.picker.picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.picker];
self.picker.hidden = NO;

}

enter image description here

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

ViewController添加UITextField的代表:

@interface ViewController : UIViewController <UITextFieldDelegate>

创建UITextFieldViewDidLoad时:

self.myTextField.delegate = self;

在您的实施文件中:

 #pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];

   // show your picker here 

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return NO; 
}