我希望在UITextfield成为FirstResponder时展示一个UIPickerView而不是键盘,并从picker view中填充值到文本字段中。有人知道如何实现吗?
使用[textfield setInputView:pickerView];
替换系统输入视图
inputView
inputAccessoryView
编辑:此回答在撰写时是正确的。自那时以来,苹果已经推出了inputView
。您现在应该使用Mafonya的答案。
可以防止键盘弹出。将您的类设置为UITextField的委托:textField.delegate = self
,并在头文件中的接口声明之后({之前)添加:<UITextFieldDelegate>
。
现在实现textFieldShouldBeginEditing:
:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return NO;
}
如果你想隐藏pickerView,这种方法就不起作用了。那么你可以创建一个UITextField的子类,并且重写选择器,在其中实现你的魔法。你可能仍然需要从返回NO,以防止键盘显示。嘿,我贴了一些之前编写的代码来涵盖这种情况。有两个例子,一个带有操作表,一个没有操作表:
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
[actionSheet showInView:displayedInView];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
这是没有使用操作表的代码:
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
for (int component = 0; component < (((NSInteger)numberOfComponentsForPickerView) - 1); component++) {
NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue];
[pickerView selectRow:valueForRow inComponent:component animated:YES];
}
[self fadeInPickerView];
[view addSubview:pickerView];
[pickerView release];
}
这个例子可以在以下链接中找到更详细的解释:http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
将输入视图添加到文本框中。
picker = [[UIPickerView alloc]init];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
MyTextField.inputView = picker;
请查看代码(包括textview+tabbar和pickerview+tabbar的代码)
UITextView和UIPickerView带有自己的UIToolbar
这将使pickerview放弃等等。然后,您只需要使用pickerview委托方法来更新文本视图即可。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
不要使用文本字段,而是使用带有白色背景的UIlabel。这样,当您点击它时,键盘将不会显示。覆盖UILabel上的触摸事件,当发生这种情况时,调用来自先前问题的方法,该方法将显示新视图。
-(void) addToViewWithAnimation:(UIView *) theView