UIPickerview Multiple TextFields Xcode

I'm looking for a way to use a single UIPickerview for two different textfields. I'd like to have the pickerview popup when each textfield is selected. After the user selects their item, the item will populate the specific text field. The picker would have to populate based on the textfield chosen.

I've read this: How to use one UIPickerView for multiple textfields in one view? and this: How to use UIPickerView to populate different textfields in one view? and this: Multiple sources for UIPickerView on textfield editing

However, none gives a complete solution.

I'm very new at Xcode so I'd like a solution that includes steps to set the storyboard also.

I appreciate any help as i've researched this for weeks.

EDIT: HERE IS MY CODE:

.h:
#import <UIKit/UIKit.h>

@interface klViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {

IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
NSMutableArray *pickerArray1;
NSMutableArray *pickerArray2;
UIPickerView *pickerView;
}
@property(nonatomic,retain) IBOutlet UITextField *textField1;
@property(nonatomic,retain) IBOutlet UITextField *textField2;
@property(nonatomic,retain) IBOutlet UIPickerView *pickerView;


@end

.m:
#import "klViewController.h"

@interface klViewController ()

@end

@implementation klViewController
@synthesize pickerView;
@synthesize textField1;
@synthesize textField2;
int variabla;

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [pickerView setHidden:YES];
    if (textField1.editing == YES) {
        [textField1 resignFirstResponder];
        [pickerView setHidden:NO];
        variabla = 1;
    }else if (textField2.editing == YES) {
        [textField2 resignFirstResponder];
        [pickerView setHidden:NO];
        variabla = 2;
    }
    NSLog(@"variabla %d",variabla);
    [pickerView reloadAllComponents];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    if (variabla == 1) {
        return [pickerArray1 count];
    }else if (variabla == 2) {
        return [pickerArray2 count];
    }else {
        return 0;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if (variabla == 1) {
        return [pickerArray1 objectAtIndex:row];
    }else if (variabla == 2) {
        return [pickerArray2 objectAtIndex:row];
    }else {
        return 0;
    }
}
- (void)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [pickerView setHidden:YES];
    pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"0", @"1", @"2", nil];
    pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", @"5", nil];
}

@end

HERE IS A SCREEN SHOT OF MY STORYBOARD:

STATUS UPDATE:

When I run the program: 1) the pickerview is hidden. 2) when I select a textfield, the picker view appears and populates correctly depending on the textfield selected.

PROBLEMS: 1) Picker doesn't go away when click outside of the textfield. 2) Textfields don't populated when a row in the Picker is selected.

Hope this provides more insight.

Answers


1) Picker doesn't go away when click outside of the textfield.

You have no code that attempts to make the picker go away when that happens. Try adding a simple tap gesture recognizer to the view. Add a line to viewDidLoad like:

[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap:)]];

Then implement the function simply like:

-(void)backgroundTap:(UITapGestureRecognizer *)tapGR{
    self.pickerView.hidden = YES;
    // And maybe..
    variabla = 0;
}

Since you are making the picker appear and disappear using the hidden property this will work very simply. There are other more sophisticated ways to do this which I hope you explore. Generally the picker is set as the textfield's inputView property; that is worth investigating.

2) Textfields don't populated when a row in the Picker is selected.

You haven't handled the picker's pickerView:didSelectRow:inComponent: delegate method. That's the method that gets called when the picker stops turning and lands on an item. Don't assume that this is the item the user selected it will be called multiple times.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *text = [self pickerView:pickerView titleForRow:row forComponent:component];
    UITextField *current = nil;
    if (variabla == 1) current = self.textField1;
    else if (variabla == 2) current = self.textField2;
    current.text = text;
}

That should get your implementation working. One more thing though variabla is an instance variable and should be declared in curly braces immediately following the @interface or @implementation line.

@implementation klViewController {
    int variabla;
}
@synt....

Give the Tag of two Different textfield for identify which textfield you select and everything is ok you just need to change below method. Hope This Work

-(void)textFieldDidBeginEditing:(UITextField *)textField  {




if ([textField viewWithTag:100]) {

    [textField resignFirstResponder];
    [self.View1 setHidden:NO];
    variable=1;
}

else if ([textField viewWithTag:101]) {

    [textField resignFirstResponder];
    [self.View1 setHidden:NO];
     variable=2;

}

    [_Picker_view reloadAllComponents];

}

Thanks :)


  1. Make sure you have declared the Picker View object in the header file.
  2. In the header file, import the UITextFieldDelegate protocol:

    @interface MyView:UIViewController < UITextFieldDelegate>

  3. In IB, set a tag for each text field you have.

  4. In the *.m file, implement the textFieldShouldBeginEditing method, update the PickerView array Data Source and Reload all the picker view components.

       -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
    
       if (textField.tag == 1) {
    
       itemsArray = [[NSArray alloc] arrayWithObjects:@"A", @"B"];
    
       }
    
       if (textField.tag == 2) {
    
           itemsArray = [[NSArray alloc] arrayWithObjects:@"Green", @"Yellow"];
       }
    
       [myPickerView reloadAllComponents];
       }
    
  5. Make sure you import the UIPickerViewDelegate and UIPickerViewDataSource in the header file.

You can use the same picker view for as many text fields as you want, to change the content of the picker view according to the selected text field you need to replace the data source of the picker view with different items whenever a text field is being selected and then reload the picker view components.

Well my friend, I'm afraid it's a little bit to complicated to explain here. You can set object's tag value in the IB properties menu. Once you dragged a PickerView into your view and it's selected, you can change the tag attribute in the Object Properties menu (on the side). I think you should look for tutorials that will show you how to setup a simple picker view, or table view (they work very similar to one another) and that will give you much more information on how to do what you want to do. In a nutshell, every Picker View takes information from a Data Source, you can create an array that contains some strings and have the picker view load each item in the array as a row. I have a small website with some beginners information, check it out. http://i-tutor.weebly.com/index.html


In .h

@interface TQViewController : UIViewController<UIPickerViewDelegate>
{
    UITextField *textfield;
    UIPickerView *Picker1;
    NSArray *Array1,*Array2;
}
end

and in .m

    - (void)viewDidLoad
    {

        [super viewDidLoad];

        //TextField
        textfield=[[UITextField alloc]initWithFrame:CGRectMake(5,5,310,40)];
        textfield.borderStyle = UITextBorderStyleRoundedRect;
        textfield.backgroundColor=[UIColor whiteColor];
        textfield.textAlignment = UITextAlignmentCenter;
        textfield.placeholder = @"<enter amount>";
        [self.view addSubview:textfield];
textfield1=[[UITextField alloc]initWithFrame:CGRectMake(5,100,310,40)];
        textfield1.borderStyle = UITextBorderStyleRoundedRect;
        textfield1.backgroundColor=[UIColor whiteColor];
        textfield1.textAlignment = UITextAlignmentCenter;
        textfield1.placeholder = @"<enter amount>";
        [self.view addSubview:textfield1];

        // PickerView1
        Array1=[[NSArray alloc]initWithObjects:@"USD",@"INR",@"EUR", nil];
        Picker1=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, 320,10)];
        Picker1.delegate=self;
        Picker1.tag=PICKER1_TAG;
        Picker1.showsSelectionIndicator=YES;
        [self.view addSubview:Picker1];
     Array2=[[NSArray alloc]initWithObjects:@"USD",@"INR",@"EUR", nil];
    }
       -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
        {
           if([textfield becomeFirstResponder])
            return [Array1 count];
           if([textfield1 becomeFirstResponder])
            return [Array2 count];
        }
        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
          NSString *title;
            if([textfield becomeFirstResponder])
            {
                title=[Array1 objectAtIndex:row];  
                return title; 
            }
            ([textfield1 becomeFirstResponder])
            {
                title=[Array2 objectAtIndex:row];  
                return title; 

            }       
        }

        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
        {

if([textfield becomeFirstResponder])
            {
                //your code 
            }
            ([textfield1 becomeFirstResponder])
            {
                //your code  

            } 
}

Need Your Help

How to import django models in scrapy pipelines.py file

python django models scrapy

I'm trying to import models of one django application in my pipelines.py to save data using django orm. I created a scrapy project scrapy_project in the first involved django application "app1" (is...