简体   繁体   English

当选择器视图选中的键盘仍然可见时

[英]when picker view selected keyboard still visible

i have a text field and picker view.when i select the text field picker view is appearing and if i select row in picker still my textfield is editable. 我有一个文本字段和选择器视图。当我选择文本字段选择器视图时,如果我在选择器中选择行,我的文本字段仍可编辑。 i want to set textfield only in drop down list.and one more problem my key board is visible when i select the textfield 我只想在下拉列表中设置文本字段。还有一个问题,当我选择文本字段时,键盘可见

   - (void)textFieldDidBeginEditing:(UITextField *)textField {


       [txtstate resignFirstResponder];//this is to hide key board
        txtstate.inputView=pickerView;


    ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(43,337, 212, 160)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(43, 0, 212, 30)];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnPressToGetValue)];
    [toolBar setItems:[NSArray arrayWithObject:btn]];

    [ViewForValuePicker addSubview:toolBar];
    pickerView = [[UIPickerView alloc]init] ;
    pickerView.frame=CGRectMake(43, 30, 212, 140);
    pickerView.delegate = self;
    pickerView.dataSource= self;
    pickerView.showsSelectionIndicator = YES;
         [ViewForValuePicker addSubview:pickerView];
    errstate.hidden=YES;
    [testScroll addSubview:ViewForValuePicker];
    [pickerView setHidden:NO];

   }


     - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
      forComponent:(NSInteger)component reusingView:(UIView *)view {
     UILabel *retval = (id)view;
         if (!retval) {
          retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
             [pickerView    rowSizeForComponent:component].width,
                     [pickerView rowSizeForComponent:component].height)] ;
   }
   retval.text = [[arr objectAtIndex:row]objectForKey:@"Code"];
    retval.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];


  return retval;
 }


     - (void)doneBtnPressToGetValue{

      [pickerView resignFirstResponder];

    [pickerView removeFromSuperview];
       txtcity.text=nil;
    autocompleteTableView.hidden=YES;
     [ ViewForValuePicker removeFromSuperview];


 }

  -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

     return 1;
    }

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
  { 
   txtLId.text=[[arr objectAtIndex:row] valueForKey:@"LId"]; 

    txtstate.text= [[arr objectAtIndex:row] valueForKey:@"Code"];
   //txtstate.userInteractionEnabled=NO;

 } 

 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 
 { 
return [arr count]; 
 } 

  - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
 { 

   return [[arr objectAtIndex:row] valueForKey:@"Code"]; 
 }

Hello try and update following code with your code or check full code at http://pastie.org/6569798 :- 您好,尝试用您的代码更新以下代码,或在http://pastie.org/6569798中检查完整代码:-

First define 首先定义

UIActionSheet *actionSheet;

in header file(.h). 在头文件(.h)中。

- (void)createActionSheet {
    if (actionSheet == nil)
    {
        actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

        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];
        [flexSpace release];

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

        [pickerToolbar setItems:barItems animated:YES];
        [barItems release];

        [actionSheet addSubview:pickerToolbar];
        [pickerToolbar release];

        [actionSheet showInView:self.view];
        [actionSheet setBounds:CGRectMake(0,0,320, 464)];
    }
}

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

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    if(textField==txtstate)
    {
        pickerView = [[UIPickerView alloc]init] ;
        pickerView.frame=CGRectMake(0, 44, 320, 420);
        pickerView.delegate = self;
        pickerView.dataSource= self;
        pickerView.showsSelectionIndicator = YES;
        [pickerView setHidden:NO];

        [self createActionSheet];
        [actionSheet addSubview:pickerView];
        [textField resignFirstResponder];
    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
          forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,
                                                          [pickerView    rowSizeForComponent:component].width,
                                                          [pickerView rowSizeForComponent:component].height)] ;
    }
    retval.text = [arr objectAtIndex:row];
    retval.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];


    return retval;
}


- (void)doneBtnPressToGetValue
{
    [txtstate resignFirstResponder];

    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
    [actionSheet release];
    actionSheet = nil;
}

Assign your textfield.inputview in viewDidLoad itself with pickerView . 使用pickerView在viewDidLoad本身中分配textfield.inputview

In textFieldDidBeginEditing - no more code required. textFieldDidBeginEditing -不需要更多代码。

Also use your UIPickerViewDelegate methods as it is. UIPickerViewDelegate原样使用UIPickerViewDelegate方法。

If you want to have just the pickerview show up when you select the text field, take all of the code you have right now in textFieldDidBeginEditing and move it into viewDidLoad . 如果要在选择文本字段时仅显示pickerview,请在textFieldDidBeginEditing您现在拥有的所有代码,并将其移至viewDidLoad If you initialize txtstate.inputView=pickerView; 如果您初始化txtstate.inputView=pickerView; before hand it will replace the keyboard as the main input. 在手动操作之前,它将取代键盘作为主要输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM