raj raj - 20 days ago 14
Objective-C Question

While using picker view

enter image description here $ The data of assigned array is not showing in the pickerView. What mistake I am doing as I haven't being able to identify my mistake.
Its .m file code:
// PickerViewController.m
// dropDownButtonTry
//

#import "PickerViewController.h"

@interface PickerViewController ()
{
NSArray *genderArray;
NSArray *cityArray;
NSArray *currentArray;
UITextField *currentTextField;
}

@end

@implementation PickerViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

cityArray = [[NSArray alloc]initWithObjects:@"Delhi",@"Mumbai",@"Chennai", nil];
genderArray = [[NSArray alloc]initWithObjects:@"Male",@"Female",@"Transgender", nil];
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;



}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self.view endEditing:YES];
}

//Needed to prevent keyboard from opening
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
currentTextField = textField;
if (textField == self.textFieldGenderOutlet) {
currentArray = genderArray;
}
if (textField == self.textFieldCityOutlet) {
currentArray = cityArray;
}
// do here everything you want
NSLog(@"Pressed on TextField!");
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;

[self.view endEditing:YES]; // Hide keyboard
NSLog(@"****current array**** %@",currentArray);
return NO;
}

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [currentArray count];

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

[currentTextField setText:[currentArray objectAtIndex:row]];


}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [currentArray objectAtIndex:row];
}

- (IBAction)textFieldGenderAction:(id)sender {
self.pickerView.hidden = NO;
self.btnDoneOutlet.hidden = NO;

}

- (IBAction)btnDone:(id)sender {
self.pickerView.hidden = YES;
self.btnDoneOutlet.hidden = YES;
}

- (IBAction)textFieldCityAction:(id)sender {
}
@end

Answer

You need to reload picker view :

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    currentTextField = textField;
    if (textField == self.textFieldGenderOutlet) {
        currentArray = genderArray;
    }
    if (textField == self.textFieldCityOutlet) {
        currentArray = cityArray;
    }
    // do here everything you want
    NSLog(@"Pressed on TextField!");
    self.pickerView.hidden = NO;
    self.btnDoneOutlet.hidden = NO;

    [self.thePicker reloadAllComponents];


    [self.view endEditing:YES]; // Hide keyboard
    NSLog(@"****current array**** %@",currentArray);
    return NO;
}
Comments