2

I got a Problem with a Picker View in Xcode 4.4. I want to populate 1 Picker with multiple arrays. I watched / read a lot of tutorials and could get almost to work. This is my code:

*.h

 #import <UIKit/UIKit.h>

@interface PickerView : UIViewController

<UIPickerViewDataSource,UIPickerViewDelegate>
{
    IBOutlet UIPickerView *picker;

    NSMutableArray *array_1;
    NSMutableArray *array_2;
}

@property (strong, nonatomic) IBOutlet UIView *pickerContainer;
@property (strong, nonatomic) IBOutlet UITextField *textfield_1;
@property (strong, nonatomic) IBOutlet UITextField *textfield_2;

- (IBAction)showArray_1Picker:(id)sender;
- (IBAction)showArray_2Picker:(id)sender;
- (IBAction)hidePicker:(id)sender;

@end

*.m

#import "PickerView.h"

@interface PickerView ()

@end

@implementation PickerView
@synthesize pickerContainer;
@synthesize textfield_1;
@synthesize textfield_2;

int textFieldTouched = 0;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent (NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 count];
            break;
        case 2:
            return [array_2 count];
            break;
        default:
            return 0;
    }
}


- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 objectAtIndex:row];
            break;
        case 2:
            return [array_2 objectAtIndex:row];
            break;
        default:
            return @"nothing";
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            textfield_1.text = [array_1 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
        case 2:
            textfield_2.text = [array_2 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
    }
}

 - (void)viewDidLoad
 {
    array_1 = [[NSMutableArray alloc] init];
    [array_1 addObject:@"1"];
    [array_1 addObject:@"2"];
    [array_1 addObject:@"3"];
    [array_1 addObject:@"4"];
    [array_1 addObject:@"5"];

    array_2 = [[NSMutableArray alloc] init];
    [array_2 addObject:@"a"];
    [array_2 addObject:@"b"];
    [array_2 addObject:@"c"];
    [array_2 addObject:@"d"];
    [array_2 addObject:@"e"];

    pickerContainer.frame = CGRectMake(0, 460, 320, 261);

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setPickerContainer:nil];
    [self setTextfield_1:nil];
    [self setTextfield_2:nil];
    [super viewDidUnload];
}

- (IBAction)showArray_1Picker:(id)sender
{
    [textfield_1 resignFirstResponder];
    textFieldTouched = 1;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)showArray_2Picker:(id)sender
{
    [textfield_2 resignFirstResponder];
    textFieldTouched = 2;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)hidePicker:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 460, 320, 261);
    [UIView commitAnimations];
}

@end

So i got 2 main problems:

  1. If i set textFieldTouched = 0 (or > 2) like i did before my picker is empty when it shows up. Anyway if I hide it again it writes the first value of the corresponding array in the textfield.

  2. If i set textFieldTouched = 1 or =2 the picker is populated with the related array no matter what text field I try to edit. But if I scroll the values on the picker out of my sight they have changed when i scroll back.

I want to have it like this:

If i touch text field 1 the picker shows up populated with array 1

If i touch text field 2 the picker shows up populated with array 2

ans so on.

Can u help me with that?

I've uploaded the code so you can look at it better.

https://dl.dropbox.com/u/5858884/ProblemCode.zip

P.S.

English is not my mother language so please excuse mistakes ;)

I started programming 2 weeks ago, so if u find something wrong in my code that doesnt belong to my problem please tell me so I can get better :)

1 Answer 1

2

In your showArray1Picker and showArray2Picker methods add the line:

[picker reloadAllComponents];

This will cause the picker to reload its data. You also need to connect the IBOutlet, picker, to the picker view in IB -- I noticed that it was not connected.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the perfect answer :)Is there a way so if I open the picker it always starts at line 1?
Do you mean, that the picker is on line 1 when you switch from one picker to the other?
i want it to be like this. at the moment if i choose the third line then hide the picker and show it again with another array the chosen line is still the third
If you add the line [picker selectRow:0 inComponent:0 animated:NO]; in your IBActions for the 2 pickers, that will force them back to the first row.
This was a great help, it solved the problem I was having because I was missing the int textFieldTouched piece of the workflow. Once that was added my problem was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.