0

テキストフィールドとピッカービューがあります。テキストフィールドを選択するとピッカービューが表示され、ピッカーで行を選択してもテキストフィールドは編集可能です。テキストフィールドをドロップダウンリストにのみ設定したいのですが、テキストフィールドを選択するとキーボードが表示されるというもう1つの問題があります

   - (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.inputviewでviewDidLoad自体を割り当てますpickerView

で -これtextFieldDidBeginEditing以上コードは必要ありません。

また、UIPickerViewDelegateメソッドをそのまま使用します。

于 2013-03-15T15:37:11.767 に答える
0

テキスト フィールドを選択したときにピッカービューだけを表示したい場合は、現在持っているすべてのコードを にtextFieldDidBeginEditing移動しviewDidLoadます。事前に初期化txtstate.inputView=pickerView;すると、キーボードがメイン入力として置き換えられます。

于 2013-03-15T15:38:02.173 に答える