点击uipickerview时键盘不会重新调整

时间:2012-10-04 05:38:22

标签: iphone ios ios4 uipickerview uikeyboard

我的搜索视图中有两个UIPickerView和一个UITextField。当我首先输入/选择两个UIPickerView然后输入UITextField时,一切正常。

但是,当我更改输入UITextField然后选择UIPickerView之类的订单时,发生的问题是当我点击UIPickerView时键盘未被重新签名。

以下是我的代码

- (void) textFieldDidBeginEditing: (UITextField *) textField
//---------------------------------------------------------------
{
    if (textField == self.txtFieldPoetName) {
        NSLog(@"sdsdfsd");
      [textField resignFirstResponder];
      if ( [self.poetNameArray count] > 0 ){
          [self showPoetNamePicker];
      }

    }
    else if (textField == self.txtFieldPoemType) {
        NSLog(@"jdjdjdjd");
        [textField resignFirstResponder];

        if( [self.poemTypeArray count] > 0 ){
            [self showPoemTypePicker];
        }
    }
}


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

    [textField resignFirstResponder];

}

- (void)showPoetNamePicker {

    [self resignKeyboard];

    startXMLParser_ = NO;

    self.actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]autorelease];
    self.actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    self.actionSheet.title = @"Poet Name";
    self.poetNamePicker = [[[UIPickerView alloc] init]autorelease];
    self.poetNamePicker.showsSelectionIndicator = YES;
    self.poetNamePicker.dataSource = self;
    self.poetNamePicker.delegate = self;
    PickerType = 1;

    //Set up the display frame
    self.poetNamePicker.frame = CGRectMake(0, 35, 320, 120);

    //Add the picker to the view
    [self.actionSheet addSubview:self.poetNamePicker];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 6.0f, 50.0f, 27.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(hidePicker:) forControlEvents:UIControlEventValueChanged];
    [self.actionSheet addSubview:closeButton];
    [closeButton release];

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

- (void)showPoemTypePicker {

    [self resignKeyboard];

    startXMLParser_ = YES;

    self.actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]autorelease];
    self.actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    self.actionSheet.title = @"Poem Type";
    self.poemTypePicker = [[[UIPickerView alloc] init]autorelease];
    self.poemTypePicker.showsSelectionIndicator = YES;
    self.poemTypePicker.dataSource = self;
    self.poemTypePicker.delegate = self;
    PickerType = 2;

    //Set up the display frame
    self.poemTypePicker.frame = CGRectMake(0, 35, 320, 120);

    //Add the picker to the view
    [self.actionSheet addSubview:self.poemTypePicker];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 6.0f, 50.0f, 27.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(hidePicker:) forControlEvents:UIControlEventValueChanged];
    [self.actionSheet addSubview:closeButton];
    [closeButton release];

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

- (void)hidePicker:(id)sender
{

  if( PickerType == 1 )
  {
    if ( [self.poetNameArray count] > 0 &&
    [self.txtFieldPoetName.text length] == 0 )
    {
        //self.poemTypeId = @"";
      self.txtFieldPoetName.text = [self.poetNameArray objectAtIndex:0];
      self.poetTypeId = [self.poetTypeIdArray objectAtIndex:0];
    }
  }
  else if( PickerType == 2 )
  {
    if ( [self.poemTypeArray count] > 0  &&
    [self.txtFieldPoemType.text length] == 0)
    {
        //self.poetTypeId = @"";
      self.txtFieldPoemType.text = [self.poemTypeArray objectAtIndex:0];
      self.poemTypeId = [self.poemTypeIdArray objectAtIndex:0];

    }
  }

  [self resignFirstResponder];
  self.actionSheet.hidden = YES;
  [self.actionSheet dismissWithClickedButtonIndex:1 animated:YES];

  if (startXMLParser_)
  {
    [self performSelector:@selector(startXMLParsing) withObject:nil afterDelay:0.5];
  }
}

-(void)resignKeyboard{

    [self.txtFieldPoetName resignFirstResponder];
    [self.txtFieldPoemName resignFirstResponder];
    [self.txtFieldPoemType resignFirstResponder];

}

4 个答案:

答案 0 :(得分:1)

编辑:

- (void) textFieldShouldBeginEditing: (UITextField *) textField
{
   if (textField == self.txtFieldPoetName)
   {
      if( [self.poemNameArray count] > 0 ){
       [self showPoetNamePicker];
      }
      return NO;
   }
   else if (textField == self.txtFieldPoemType)
   {
     if( [self.poemTypeArray count] > 0 ){
        [self showPoemTypePicker];
     }
     return NO;
   }
   else
   {
     return YES;
   }
}

在showPoetNamePicker中添加:

 - (void)showPoetNamePicker
 {
     [self.view endEditing:YES];
     ........
     ........
 }

答案 1 :(得分:0)

您是否已正确链接到委托?

答案 2 :(得分:0)

尝试更改此方法,

 -(void)resignKeyboard{

     [self.view endEditing:YES];

}

答案 3 :(得分:0)

我知道这是一个老问题,但我认为应该提到......我认为更好的解决方案是使用每个textField的inputView(以及可选的inputAccessoryView)。这样的事情:

-(void)setupTextFields{
  UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(endEditing)], nil];

  self.txtFieldPoetName.inputView = self.poetNamePicker;
  self.txtFieldPoetName.inputAccessoryView = toolbar;
  self.txtFieldPoemType.inputView = self.poetTypePicker;
  self.txtFieldPoemType.inputAccessoryView = toolbar;
}

-(void)endEditing{
  [self.txtFieldPoetName resignFirstResponder];
  [self.txtFieldPoemType resignFirstResponder];
}

操作系统确保切换键盘的选择器,反之亦然(它与键盘显示的视图相同)。