Reputation: 141
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:
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.
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 :)
Upvotes: 1
Views: 2995
Reputation: 104082
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.
Upvotes: 2