UItextfield与pickerview作为firstresponder ios
我有一个UITextField,当点击需要显示UIPickerView。此外,在pickerview中选择的数据将被插入到文本框中。为了达到目的,我现在使用subhajit的SHPickerField。UItextfield与pickerview作为firstresponder ios
我已经创建的UITextField的一个子类,很容易实施和使用。这里是GitHub的链接:
访问https://github.com/subhajitregor/SHPickerFieldExample
请看例子来看看如何使用。自述文件现在也被更新。
无法运行项目 –
如果您使用较低版本的Xcode而不是Xcode 8,请将您的目标更改为iOS 9 –
感谢您的工作 –
试试这个
let myPicker = UIPickerView()
@IBOutlet var myText: UITextField!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
myText.inputView = myPicker
}
@IBAction func textPiker(_ sender: UITextField) {
sender.inputView = myPicker
}
借助此代码,您还可以打开多个文本字段。
.H
UIPickerView *monthPickerView;
@property (strong, nonatomic)NSArray *monthArray;
@property (retain, nonatomic) IBOutlet UITextField *monthTextField;
.M
self.monthArray=[[NSArray alloc] initWithObjects:@"one",@"two", nil];
self.monthTextField.delegate=self;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self showPicker:textField];
else if(textField==_monthTextField){
if(self.monthTextField.text.length>=1){
[monthPickerView selectRow:[self.monthArray indexOfObject:self.monthTextField.text] inComponent:0 animated:NO];
}
}
return YES;
}
//**************show picker**************
- (IBAction)showPicker:(id)sender {
UITextField *textField=(UITextField *)sender;
monthPickerView = [[UIPickerView alloc] init];
monthPickerView.showsSelectionIndicator = YES;
monthPickerView.dataSource = self;
monthPickerView.delegate = self;
pickerToolbar = [[UIToolbar alloc] init];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneClicked:)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
if(textField==_monthTextField){
monthPickerView.tag=;
self.monthTextField.inputView = monthPickerView;
self.monthTextField.inputAccessoryView = pickerToolbar;
}
}
-(void)doneClicked:(id) sender
{
[self.view endEditing:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag==1){
self.monthTextField.text = [_monthArray objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if(pickerView.tag==1){
return [_monthArray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if(pickerView.tag==1){
return [_monthArray objectAtIndex:row];
}
return 0;
}
可以显示乌尔TRID代码 –
欢迎*上。请注意,这不是一个免费的代码写入服务,但我们渴望帮助其他程序员(和有志之士)编写他们自己的代码。请阅读[如何提出一个好问题](http://*.com/help/how-to-ask)上的帮助主题。之后,请用您迄今编写的代码更新您的问题,以完成您希望实现的任务。 –
兴奋地你想知道什么? –