I have 8 textfields in one view. I want to populate each using a pickerview. Can I have 1 pickerview which will display different list of items from different arrays for different textfields?
Can someone explain how do I do it programmatically with a sample code?
Also how can I hide the pickerview when the view loads and it should be displayed only when I click on a textfield with its corresponding data list? It should disappear when I select the data and reappear when I click on a different textfield with its corresponding data list and so on.
I am new to xcode. Any help will be much appreciated. Thank you.
You can use only one PickerView that shows different data depending on some conditions.
My idea is the following: you could create, lets say, 8 different arrays - one for populating the UIPickerView depending on what UITextField was tapped. Declare them in the interface as NSArray and initialize in viewDidLoad:
array1st = ...
array2nd = ...
array3d = ...
//and until 8.
Then you create another just to point the array that should fill it (like NSArray *currentArray
) and you don't even need to init it = it will be used only for pointing the correct array.
So you should set the delegate of the UITextFields to your view controller, and in the method textFieldShouldBeginEditing
you check who is the UITextField, assign the correct array as the currentArray, reload the UIPickerView with reloadData
, and return NO in the same textFieldShouldBeginEditing
method. currentTextField is a pointer only to know what is the currentUITextField being "edited" - we need to store it to be able to set the text after selecting data in the picker. Declare currentTextField in the interface.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
currentTextField = textField;
if (textField == myFirstTextView)
{
currentArray = array1st;
[pickerView reloadData];
[self animatePickerViewIn];
return NO;
}
//and this way until 8th
}
So, when your view controller, that in fact is the UIPickerViewDelegate, you populate your UIPickerView according to the current array and you don't need to change anything, just use the currentArray as the array to get data from.
Now, to show the pickerView:
After connecting the IBOutlet, in your viewDidLoad you should do two things: set the frame of the UIPickerView and add it as a subview:
pickerView.frame = CGRectMake(0,self.view.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height);
pickerView.delegate = self;
pickerView.dataSource = self;
//or you can do it via Interface Builder, right click the pickerView and then connect delegate and dataSource to the file's owner
[self.view addSubview:pickerView];
Now you need to create the method called animatePickerViewIn
, that we called when the user taps the UITextField.
-(void)animatePickerViewIn
{
[UIView animateWithDuration:0.25 animations:^{
[pickerView setFrame:CGRectMake(0, pickerView.frame.origin.y-pickerView.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height)];
}];
}
To load the data in the pickerView:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [currentArray count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [currentArray objectAtIndex:row];
}
And when the user selects the row in the pickerView, you will receive it as a delegate method. So just set the text of the currentTextField as the value selected:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//and here you can do in two ways:
//1
[currentTextField setText:[currentArray objectAtIndex:row]];
//2
[currentTextField setText:[self pickerView:pickerView titleForRow:row inComponent:component]];
}
Use This.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
MyPickervie.delegate=self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView;
{
return 1;
}
-(void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component;
{
switch(textfield.tag){
Case 0:
label.text=[arraNo objectAtindex:row];
break;
default: break;
}
}
-(NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView numberOfRowsInComponent:(NSInteger)component;
{
switch(textfield.tag){
Case 0:
return [arrayNo count];
break;
default: break;
}
}
-(NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
switch(textfield.tag){
Case 0:
return [arrayNo objectAtIndex:row];
break;
default: break;
}
}
Where you have to take 8 Cases in switch condition to call different array and set it into the pickerView.
I have tested this code with 3 textfields.
And found perfect solution of your problem.
1) Take one global UITextField.
2) On delegate method of textfield edit, set global textfield to current textfield.
3) On picker value change method, set the text of global textfield and it'll change the value of selected textfield.
Below code is working properly.
Enjoy coding.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:@"Test %d",row + 1];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
test.text = @"XYZ";
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
test = textField;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With