xcode 如何在uitextfield而不是键盘中显示pickerview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/906112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show pickerview in uitextfield not the keyboard?
提问by Rahul Vyas
I want to show a UIPickerView on becoming FirstResponder of a UITextfield not the keyboard and fill the value in text field form the picker view.
我想在成为 UITextfield 而不是键盘的 FirstResponder 时显示 UIPickerView,并在选择器视图的文本字段中填充值。
any one knows this?
有人知道吗?
回答by mafonya
Use [textfield setInputView:pickerView];
使用 [textfield setInputView:pickerView];
Replacing the System Input Views
inputView
inputAccessoryView
替换系统输入视图
inputView inputAccessoryView
回答by klaaspieter
Edit:this answer was correct at the time of writing. Apple has since introduced inputView
. Mafonya answeris what you should be doing nowadays.
编辑:这个答案在撰写本文时是正确的。苹果公司此后推出了inputView
. Mafonya 的答案是您现在应该做的事情。
It is possible to prevent the keyboard from popping up. Set your class to be the delegate of the UITextField: textField.delegate = self
and add: <UITextFieldDelegate>
after your interface declaration (before the {) in the header file.
可以防止键盘弹出。将您的类设置为 UITextField: 的委托,textField.delegate = self
并<UITextFieldDelegate>
在头文件中的接口声明之后(在 { 之前)添加:。
Now implement textFieldShouldBeginEditing:
:
现在实施textFieldShouldBeginEditing:
:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return NO;
}
If you want to be able to hide your pickerView this method won't work. What you could do then is create subclass of UITextField and override the *trackingWithTouch:event:
selectors and do your magic in those. You probably still need to return NO from textFieldShouldBeginEditting:
to prevent the keyboard from showing.
如果您希望能够隐藏您的 pickerView,则此方法将不起作用。然后你可以做的是创建 UITextField 的子类并覆盖*trackingWithTouch:event:
选择器并在其中发挥你的魔力。您可能仍然需要返回 NO fromtextFieldShouldBeginEditting:
以防止键盘显示。
回答by williamb
Hey I have pasted in some code I wrote a while back to cover this scenario. There are two examples one with an actionsheet and one without an actionsheet:
嘿,我已经粘贴了一些我写了一段时间的代码来涵盖这种情况。有两个示例,一个带有操作表,一个没有操作表:
- (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)];
}
This is the code without the actionsheet:
这是没有操作表的代码:
- (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];
}
An more detailed example of this can be found at: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
可以在以下位置找到更详细的示例:http: //www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
回答by oscar castellon
add inputview to textField.
将输入视图添加到文本字段。
picker = [[UIPickerView alloc]init];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
MyTextField.inputView = picker;
回答by Bluephlame
Check out the code at (there is both textview+tabbar and pickerview+tabbar code)
查看代码(有 textview+tabbar 和 pickerview+tabbar 代码)
UITextView and UIPickerView with its own UIToolbar
带有自己的 UIToolbar 的 UITextView 和 UIPickerView
this will get the pickerview to resign etc. All u need to do then is use the pickerview delegate method to update the text view
这将使pickerview辞职等。所有你需要做的就是使用pickerview委托方法来更新文本视图
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Instead of using a textfield, use a UIlabel, with a white background. That way the keyboard will not show when you tap it. override the touches event on the UILabel, when that happens call the method form the privious question that will display the new view.
不要使用文本字段,而是使用带有白色背景的 UIlabel。这样,当您点击它时,键盘将不会显示。覆盖 UILabel 上的 touches 事件,当发生这种情况时,调用将显示新视图的原始问题的方法。
-(void) addToViewWithAnimation:(UIView *) theView