2

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:

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.

Community
  • 1
  • 1
Shaun Wright
  • 115
  • 4
  • 13
  • 1
    Wat do u really want to achieve ?? – IronManGill Oct 10 '12 at 05:56
  • I'm looking to populate two pickerviews from a pre-defined list (i.e. sports and categories). When the user clicks on a textfield, a uipicker view will pop up with the list (i.e. sports). When a sport is selected, the pickerview will go away and the textfield will show the sport. Same goes for the category. Does that help? – Shaun Wright Oct 10 '12 at 06:11
  • So you would have 2 textfields in that case ... but u want to use that one pickerview only rite ?? – IronManGill Oct 10 '12 at 06:12
  • So i want to know: 1) how to link via storyboard the textfields to the pickerview and the textfields/pickerview to delegates, etc., 2) display pickerview when textfield is selected 3) populate the pickerview from a predefined array and 4) populate the textfield from the selection in the pickerview – Shaun Wright Oct 10 '12 at 06:13
  • To answer your previous question, yes: two textfields, one pickerview. – Shaun Wright Oct 10 '12 at 06:14
  • @ShaunWright to be honest your question as is, which includes the statement "However, none gives a complete solution." , reads something like 'I was unable to simply copy & paste one of these answers; so could someone write an answer that I can.' You have been researching for weeks, why not show us what you have so far? Then we could point out the problems and help you get going. That is the purpose of StackOverflow; helping people overcome their programming errors, not programming for them. – NJones Oct 10 '12 at 15:54
  • @NJones, I've tried many solutions and nothing has worked so I deleted the projects. I will try to implement the code and report back with errors. – Shaun Wright Oct 10 '12 at 18:32
  • @ShaunWright Much better. I have posted an answer to address your two issues. – NJones Oct 11 '12 at 16:09

4 Answers4

2

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....
NJones
  • 27,139
  • 8
  • 70
  • 88
  • Thanks NJones. That helped. the inputview property is def worth doing -- i'd prefer it but I implemented this code first and honestly, don't want to spend days figuring it out... – Shaun Wright Oct 11 '12 at 18:08
  • NJones, as a next step I'm looking to parse json data into the array. I created this post: http://stackoverflow.com/questions/12846093/parse-json-encoded-data-into-an-array-to-be-used-for-a-picker-view. Could you take a look at it? – Shaun Wright Oct 11 '12 at 18:22
1

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 :)

0

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  

            } 
}
Venk
  • 5,949
  • 9
  • 41
  • 52
0
  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

Shachar
  • 1,110
  • 9
  • 22
  • I apologize for my limited understanding of xcode (and obj-c for that matter). How do I set a tag for each textfield? Also, what do you mean update the PickerView Array Data Source and ReLoad all the picker view components? Can you provide the entire .h and .m files? Is this a new class I need to create in my code? Once I add the .h and .m class files, how do I connect those files to a pickerview that I drag into the storyboard? While the information you have provided is helpful, There is no way I can create the code on my own to fill the holes. – Shaun Wright Oct 10 '12 at 18:24
  • I've added some info on my answer. hope it helps. – Shachar Oct 11 '12 at 04:04