Dynamic UIPickerView with multiple TextField - ios

I am using UIPickerView with UITextField. It is working when I use only one UITextField.
What I want is to use multiple TextField for one dynamic pickerview.
- (void)viewDidLoad
{
[super viewDidLoad];
picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;
self.transitTF.inputView = picker;
self.theData = #[#"one",#"two",#"three",#"four"];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.theData.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.theData[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.transitTF.text = self.theData[row];
[self.transitTF resignFirstResponder];
}
This is what I use with one textField only.
Any help will be appreciated. Thanks.

Add tag property for each UITextField. In this case self.pickerView.inputView.tag you may use to find which textfield is using in current moment and you may fill picker with correct values

Related

User defined picker view to display 12 digits in it

I want to use picker view to display 12 digits in it that digit comes dynamic and it has to be light flight schedule display . Digits will be increasing always that i want to make it through picker. As i am new to IOS need your help. Thank you
#import "DetailViewController.h"
#interface DetailViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerview = [[UIPickerView alloc]init];
pickerview.delegate = self;
pickerview.dataSource = self;
[self.view addSubview:pickerview];
}
- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 12;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSNumber *digit = #(row);
return [digit stringValue];
}
#end
You can use Simple Library and Then Add array of Digits .
here are Few Simple Library , try it once.
CPPickerView
ActionSheetPicker-3.0
AKPickerView

Custom UITextLabel with picker wont display numbers when tapped on

Similar to how amazon does for selecting quantity of an item. I want it to then save the number to display in another tableview. Here is the code I have for my .m file. When I click on the text field the picker is empty.
#interface ApparelViewController ()
#property(nonatomic, readonly) NSInteger numberOfComponents;
#end
#implementation ApparelViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *picker = [[UIPickerView alloc] init];
self.quantity.inputView = picker;
//creating the arrays for the values in the picker wheel
self.quantity0 = #[#1, #2, #3, #4, #5];
}
//custom picker in text field
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.quantity0.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component {
return self.quantity0[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.quantity.text = self.quantity0[row];
[self.quantity resignFirstResponder];
}
Looks like you forgot to set the UIPicker's delegate to self.
In your viewDidLoad, try this:
picker.delegate = self;

Selecting values from the second picker column

I am using a uipicker app where I have two columns of selected data, using this code I can select the values fromm the picker (when it only has one row)
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerData = #[#"item1", #"item2", #"Item 4", #"Item 5", #"Item 6"];
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
// return 2;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row];
// return _pickerData[row][component];
}
- (IBAction)selOptions:(id)sender {
NSInteger row = [self.picker selectedRowInComponent:0];
NSString *selected = [_pickerData objectAtIndex:row];
NSLog(selected);
}
#end
With this code when I click my button I have the value of whatwas selected and then I can go on to push my segue or whatever I like.
However, I would like mypicker to have two rows, but with the code posted below...I cannot I get an error when I click the button.
- (void)viewDidLoad
{
[super viewDidLoad];
_pickerData = #[ #[#"item1", #"A"],
#[#"item2", #"B"],
#[#"item3", #"C"],
#[#"item4", #"D"],
#[#"item5",#"E"],
#[#"item6",#"F"]
];
// Connect data
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
// return 1;
return 2;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row][component];
}
- (IBAction)selOptions:(id)sender {
NSInteger row = [self.picker selectedRowInComponent:0];
NSString *selected = [_pickerData objectAtIndex:row];
NSLog(selected);
NSInteger row2=[self.picker selectedRowInComponent:1];
NSString *selectedb=[_pickerData objectAtIndex:row2];
NSLog(selectedb);
}
#end
The data is displayed fine but, I do not know how to allow for values from both of my rows to be selected upon clicking my button.
Any help on this would be appreciated.
Thank you

Using UIPickerView to populate UITextView within a UIViewController .xib framework

Let me preface by saying that I am completely new to programming. I have also done a bunch of searching on this issue and haven't found an answer that seems to address exactly what I am doing.
The result I am looking for is a text field that can be populated by the user by using a pickerView. After putting a UITextView box in the .xib file I am not sure how make it call a pickerView when tapped and then populate from that picker view.
Thank you for any help given. Let me know if I need to clarify more.
Create a UIPickerView and set it as the inputView property of your UITextView. Then, register your class for the dataSource/delegate of the UIPickerView to populate it and know when it has changed selection. Below is a simple solution for a picker with a single component.
SomeViewController.h:
#interface SomeViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
UIPickerView *picker;
NSArray *dataSource;
}
SomeViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;
myTextView.inputView = picker;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataSource count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [dataSource objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[myTextView setText:[dataSource objectAtIndex:row]];
}

iOS - Populate a static UIPickerView

I have a profile form as part of a registration process for an iOS app.
I would like to use a 'drop down' menu for items such as gender, title, DOB etc. The data for each will be static - I will use the UIPickerView to implement - but my question is - do I need to create arrays and data delegates to populate each individual picker or is there a simpler way to apply static data?
Can you do it without a delegate? No.
Can you do it without an array? Yes, but you shouldn't.
Here is an example without an array (from http://cocoamatic.blogspot.com/2010/08/create-uipickerview-programmatically.html).
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = [#"" stringByAppendingFormat:#"Row %d",row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
However, you really should just use an array it makes it so much easier to read an maintain. If you want to add an additional value, you just add it to your array. Instead of updating in multiple spots, just add another value to your array. Also, much easier to understand.
#implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray = #[#"Row 1", #"Row 2", #"Row 3",];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 3;
return _myArray.count;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = myArray[row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
If you had multiple pickers, you would just use multiple arrays and check each PickerView to see which one you had since the PickerView is passed into each of the functions listed.
#implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray1 = #[#"Row 1", #"Row 2", #"Row 3",];
_myArray2 = #[#"Row 1-2", #"Row 2-2", #"Row 3-2", #"Row 4-2"];
UIPickerView pickerView1 = [[UIPickerView alloc] init];
UIPickerView pickerView2 = [[UIPickerView alloc] init];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
if (pickerView == pickerView1)
{
// First Picker
}
else if (pickerView == pickerView2)
{
// First Picker
}
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return _myArray1.count;
}
else if (pickerView == pickerView2)
{
// Second Picker
return _myArray2.count;
}
// A third picker passed in somehow
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (pickerView == pickerView1)
{
// First Picker
return 1;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 1;
}
// A third picker passed in somehow
return 0;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if (pickerView == pickerView1)
{
// First Picker
title = myArray1[row];
}
else if (pickerView == pickerView2)
{
// Second Picker
rtitle = myArray2[row];
}
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (pickerView == pickerView1)
{
// First Picker
return 300;
}
else if (pickerView == pickerView2)
{
// Second Picker
return 400;
}
// A third picker passed in somehow
return 0;
}

Resources