I have a problem with my pickerview where by clicking on the textfield it shows an empty uipickerview. I have 3 textfield where by clicking on each textfield there will be different array for each one. Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
tbMode.inputView = pickerView;
tbWalkDist.inputView = pickerView;
tbRouteOpt.inputView = pickerView;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField*)textField;
{
modeArray = [[NSMutableArray alloc] init];
if(textField.tag==1){
[modeArray addObject:@" A "];
[modeArray addObject:@" B "];
pickerView.hidden = NO;
}
else{
if(textField.tag==2){
[modeArray addObject:@" 300 "];
[modeArray addObject:@" 400 "];
[modeArray addObject:@" 500 "];
pickerView.hidden = NO;
}
else{
if(textField.tag==3){
[modeArray addObject:@" cheapest "];
[modeArray addObject:@" fastest "];
pickerView.hidden = NO;
}
else{
pickerView.hidden=YES;
}
}
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
tbMode.text = [modeArray objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [modeArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [modeArray objectAtIndex:row];
}
pls help! where did i got wrong? is it possible that this method -(void)textFieldDidBeginEditing:(UITextField*)textField;
is not called? thanks
edit* i miss out these 3 line of line which is for each textfield which use to call that method in viewDidLoad
tbMode.delegate = self;
tbWalkDist.delegate = self;
tbRouteOpt.delegate = self;
But I'm still getting an empty picker view. how can i add these NSMutableArray
into the the picker view
*add in [pickerView reloadAllComponents];
the picker will display the values :)
As for your output problem, you need to keep track of the uiTextField that activated. Add an ivar,
uiTextField *activeField;
set the variable to the appropriate uiTextField object in the textfield delegate method. Then replace,
tbMode.text = [modeArray objectAtIndex:row];
with,
activeField.txt = [modeArray objectAtIndex:row];
Set the ivar like so:
-(void)textFieldDidBeginEditing:(UITextField*)textField
{
activeField = textField;
// all the if else logic here
}