点击 uipickerview 时键盘不退出
Posted
技术标签:
【中文标题】点击 uipickerview 时键盘不退出【英文标题】:Keyboard is not resigning when tapping uipickerview 【发布时间】:2012-10-04 05:38:21 【问题描述】:我的搜索视图中有两个UIPickerView
和一个UITextField
。当我首先输入/选择两个UIPickerView
然后我输入UITextField
时,一切正常。
但是当我改变输入UITextField
然后选择UIPickerView
的顺序时,发生的问题是当我点击UIPickerView
s时键盘没有退出。
下面是我的代码
- (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];
【问题讨论】:
你说你有一个 textField 但正在寻找 3 textField txtFieldPoetName, txtFieldPoemName, txtFieldPoemType 【参考方案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];
........
........
【讨论】:
解决了因为我弄错了应该是textFieldShouldBeginEditing 点击 textfiled 时显示键盘,但点击 uipickerview 时不显示选择器 uipickers 和 textfield 都必须工作。我的问题是当点击 uitextfield(不是 uipicker)时键盘来了。这很好。当点击上面的 uitextfiled 这是一个 uipicker键盘未隐藏,并且通过重叠键盘来显示选择器视图。这就是问题【参考方案2】:您是否正确链接到委托?
【讨论】:
【参考方案3】:尝试改变这个方法,
-(void)resignKeyboard
[self.view endEditing:YES];
【讨论】:
【参考方案4】:我知道这是一个老问题,但我认为应该提到它......我认为更好的解决方案是使用每个 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];
操作系统确保为键盘切换选择器,反之亦然(我猜它显示在与键盘相同的视图中)。
【讨论】:
以上是关于点击 uipickerview 时键盘不退出的主要内容,如果未能解决你的问题,请参考以下文章
使用 UITextField + UIPickerview 时禁用键盘输入
像键盘一样显示UIPickerView,没有UITextField