键盘和PickerView都是可见的

时间:2013-04-29 08:28:00

标签: iphone

我有一个文本域,我已经实现了UIPickerView并使用resignFirstResponder禁用了键盘,但是当我点击另一个时,键盘和选择器视图都可见。我这样排序吗?

6 个答案:

答案 0 :(得分:2)

创建UIPickerView的本地实例,并在文本字段textFieldDidBeginEditing的委托方法中检查UIPickerView的可见性,如果是可见隐藏

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(![_pickerView isHidden])
    {
        [_pickerView setHidden:YES];
    }
    //...
}

答案 1 :(得分:1)

您需要知道要准确显示的内容。您可以使用[self.view endEditing:YES];在必要时删除键盘,并在需要时实施自己的方法来隐藏选择器。

答案 2 :(得分:1)

将每个UITextField的委托设置为self,然后将此方法放在.m类中。在此代码中,我只需将yourPickerView的框架设置为{ {1}} 500点而不是您可以隐藏y ..

此代码将UIPickerView隐藏为类似键盘的动画..

<强>更新

UIPickerView

答案 3 :(得分:1)

将选择器设置为文本字段的inputView

myTextField.inputView = self.myPickerView;

这样您根本不需要处理键盘/选择器的显示。这一切都是为你完成的。

答案 4 :(得分:1)

最简单的解决方案: inputView Property

yourTextField.inputView = self.yourPickerView;

检查Documentation

答案 5 :(得分:0)

当您要显示PickerView

时,您需要将此行放在第一位
[self.view endEditing:YES];

此后PickerView将打开&amp;在写下面的行后,焦点不会出现在TextField上,所以你需要做的就是将Done Button放在Picker&amp;将Picker的Selected值传递给该TextField。

试用此代码:

- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
    if(txtVisitDate.isEditing == TRUE)
    {
        if ( [txtPlaceName isFirstResponder] )
        {
            [txtPlaceName resignFirstResponder];
        }
        [aTextField resignFirstResponder];  

        pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
        pickerView.datePickerMode = UIDatePickerModeDate; 
        pickerView.hidden = NO;
        pickerView.date = [NSDate date];  

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
        [pickerToolbar sizeToFit];  

        NSMutableArray *barItems = [[NSMutableArray alloc] init];    
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
        [barItems addObject:flexSpace];  

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
        [barItems addObject:doneBtn];  

        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
        [barItems addObject:cancelBtn];  

        [pickerToolbar setItems:barItems animated:YES];  

        [pickerViewPopup addSubview:pickerToolbar];  
        [pickerViewPopup addSubview:pickerView];  
        [pickerViewPopup showInView:self.view];  
        [pickerViewPopup setBounds:CGRectMake(0,0,320, 475)];  
    }
}

希望这会有所帮助。