30178

Adding UIPicker to textfield

Question:

I have a table view & I have to display text fields in some cells. When user click on the text field, I want the picker view to be displayed with integer values from 0 to 99. once the user selects the item in picker view the same needs to be displayed on that particular text field. Any help would be sincerely appreciated.

Answer1:

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

Add target of this line to ur textfield 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; }

Answer2:

Swift iOS: (Note: please change vars name by requirement)

Step 1 : declare vars and objects

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)

Step 2: declare delegates and protocol:

// 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 }

Answer3:

Create a picker view and implement the delegate and datasource methods in your view controller. Set the picker view as the inputview of your text field. It will then pop up instead of the keyboard when the user edits the text field.

Use the didSelect... delegate methods to update the text field value when the user alters the picker.

Answer4:

First in the viewDidLoad method you need to instantiate the PickerView and to add it to the textField

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

Then you have to implement the UIPickerView delegate

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

Where arrayWithData is the array with the source for the PickerView.

Recommend

  • How to show different clock from different time zones in iPhone app
  • do I have to release the NSDate in this code below?
  • Direction of target annotation when outside of visible area
  • Badge value on “More” tab
  • access an alertView's calling view
  • Why does Apple use assign rather than weak to store a delegate?
  • Laravel lmutator $this->attributes return 'Undefined index: id'
  • Oracle ListaGG, Top 3 most frequent values, given in one column, grouped by ID
  • get all processes in parallel
  • Using an array for autofilter criteria
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Count from each distinct date, fill in missing dates with zero
  • Opaque reference instead of PImpl. Is it possible?
  • Count New Lines in Text File
  • Cannot find “Grammar.txt” in python-sphinx
  • Creating NSCollectionView with datasource programatically
  • SQL: Getting the physical size of a subset of a table
  • C# Excel interop - how to test if interop object is still working and performing a task?
  • Why does java tzupdater add leap seconds?
  • Add dynamic data to line chart from mysql database with highcharts
  • Using Sax parsing to edit and write XML in VB6
  • Build Successful but not running on simulator
  • how to save the state in userdefaults of accessory checkmark-iphone
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Django: Count of Group Elements
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Trying to switch camera back to front but getting exception
  • Getting 'uninitialized constant' error when using delegate in belongs_to in model
  • Suggestions to manage Login/Logout transitions
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?