将UIPicker添加到文本字段

我有一个表格视图,我必须在一些单元格中显示文本字段。 当用户单击文本字段时,我希望select器视图以0到99之间的整数值显示。一旦用户在选取器视图中select了该项目,则需要在该特定文本字段上显示相同的内容。 任何帮助将真诚感激。

-(void) viewDidLoad { arrayNo = [[NSMutableArray alloc] init]; [arrayNo addObject:@"1"]; [arrayNo addObject:@"2"]; [arrayNo addObject:@"3"]; // and so on.... } 

将这一行的目标添加到你的文本框function

  UITextfield *entered2; //..... some codes [entered2 addTarget:self action:@selector(showPicker)forControlEvents:UIControlEventEditingDidBegin]; [myview addSubview:entered2]; -(void) showPicker { [entered2 resignFirstResponder]; pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,240,220,0)]; pickerView.delegate = self; pickerView.dataSource = self; pickerView.showsSelectionIndicator = YES; [self.view addSubview:pickerView]; [pickerView release]; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [arrayNo count]; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { entered2.text=[arrayNo objectAtIndex:row]; [pickerView removeFromSuperview]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [arrayNo objectAtIndex:row]; [pickerView removeFromSuperview]; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { int sectionWidth = 300; return sectionWidth; } 

Swift iOS:(请注意:请按要求更改名称)

步骤1:声明variables和对象

 var objPickerView : UIPickerView = UIPickerView() // select the picker view data values objPickerView.delegate=self; objPickerView.dataSource=self; objPickerView.showsSelectionIndicator=true; // show picker view when text field clicked self.smokingStatusText!.inputView = objPickerView // reload the component of picker self.objPickerView.reloadAllComponents() // select the value of picker status(Which row must be selected) self.objPickerView.selectRow(sStatus!, inComponent: PickerComponent.size.rawValue, animated: false) 

第2步:声明代表和协议:

 // programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 } // programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData.count } //MARK: UIPickerView Delegates // programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { smokingStatusText!.text = pickerData[row] self.smokerStatusNum = String(row) } // programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let titleData = pickerData[row] let font:UIFont? = UIFont(name: "Georgia", size: 14.0) let attrString = NSAttributedString( string: titleData, attributes: NSDictionary( object: font!, forKey: NSFontAttributeName)) return attrString } // programming mark ----- ---- ----- ----- ---- ----- ----- ---- ----- //size the components of the UIPickerView func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 36.0 } func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return 300 } 

创build一个select器视图,并在视图控制器中实现委托和数据源方法。 将选取器视图设置为文本字段的inputview视图。 当用户编辑文本字段时,它将popup而不是键盘。

当用户改变select器时,使用didSelect...委托方法来更新文本字段值。

首先在viewDidLoad方法中,您需要实例化PickerView并将其添加到textField

 UIPickerView *pickerView =[[UIPickerView alloc]init]; pickerView.delegate=self; pickerView.dataSource=self; pickerView.showsSelectionIndicator=YES; myTextField.inputView=pickerView; 

然后你必须实现UIPickerView委托

 - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { myTextField.text=[arrayWithData objectAtIndex:row]; } 

其中arrayWithData是具有PickerView的源的数组。