0

我有一个文本字段和选择器视图。当我选择文本字段选择器视图时,如果我在选择器中选择行,我的文本字段仍然是可编辑的。我只想在下拉列表中设置文本字段。还有一个问题,当我选择文本字段时,我的键盘是可见的

   - (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"]; 
 }
4

3 回答 3

1

您好,尝试使用您的代码更新以下代码,或在http://pastie.org/6569798查看完整代码:-

首先定义

UIActionSheet *actionSheet;

在头文件(.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;
}
于 2013-03-15T17:29:34.690 回答
0

用 .分配你的textfield.inputviewin viewDidLoad 本身pickerView

In textFieldDidBeginEditing- 无需更多代码。

UIPickerViewDelegate也按原样使用您的方法。

于 2013-03-15T15:37:11.767 回答
0

如果您希望在选择文本字段时只显示选择器视图,请将您现在拥有的所有代码textFieldDidBeginEditing移入viewDidLoad. 如果您事先初始化txtstate.inputView=pickerView;,它将取代键盘作为主要输入。

于 2013-03-15T15:38:02.173 回答