【发布时间】:2009-05-25 09:55:31
【问题描述】:
我想在成为 UITextfield 的 FirstResponder 而不是键盘时显示 UIPickerView,并在选择器视图的文本字段中填充值。
有人知道吗?
【问题讨论】:
标签: iphone xcode uitextfield uipickerview
我想在成为 UITextfield 的 FirstResponder 而不是键盘时显示 UIPickerView,并在选择器视图的文本字段中填充值。
有人知道吗?
【问题讨论】:
标签: iphone xcode uitextfield uipickerview
使用 [textfield setInputView:pickerView];
替换系统输入视图
inputView
inputAccessoryView
【讨论】:
编辑:此答案在撰写本文时是正确的。此后,Apple 推出了inputView。 Mafonya answer 是你现在应该做的。
可以防止键盘弹出。将您的类设置为 UITextField 的委托:textField.delegate = self 并在头文件中的接口声明之后({ 之前)添加:<UITextFieldDelegate>。
现在实现textFieldShouldBeginEditing::
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return NO;
}
如果您希望能够隐藏您的 pickerView,此方法将不起作用。然后你可以做的是创建 UITextField 的子类并覆盖 *trackingWithTouch:event: 选择器并在其中发挥你的魔力。您可能仍需要从 textFieldShouldBeginEditting: 返回 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/
【讨论】:
将 inputview 添加到 textField。
picker = [[UIPickerView alloc]init];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
MyTextField.inputView = picker;
【讨论】:
查看代码(有textview+tabbar和pickerview+tabbar代码)
UITextView and UIPickerView with its own UIToolbar
这将使pickerview辞职等。然后你需要做的就是使用pickerview委托方法来更新文本视图
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
不要使用文本字段,而是使用带有白色背景的 UIlabel。这样,当您点击它时,键盘将不会显示。覆盖 UILabel 上的 touches 事件,当发生这种情况时,调用方法形成将显示新视图的 privious 问题。
-(void) addToViewWithAnimation:(UIView *) theView
【讨论】: