0

我的搜索视图中有两个UIPickerView和一个UITextField。当我UIPickerView先输入/选择这两个然后输入UITextField.

但是当我像 InputtingUITextField然后selection 这样更改顺序时,发生的问题是当我点击sUIPickerView时键盘没有退出。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

4 回答 4

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];
     ........
     ........
 }
于 2012-10-04T05:43:32.767 回答
0

您是否正确链接到委托?

于 2012-10-04T05:47:06.517 回答
0

尝试改变这个方法,

 -(void)resignKeyboard{

     [self.view endEditing:YES];

}
于 2012-10-04T05:48:29.833 回答
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];
}

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

于 2014-02-10T21:48:58.010 回答