Mohamad Afiq Mohamad Afiq - 27 days ago 9
iOS Question

How to use one UIPickerView for two textfields in one view?

I have 2 textfields in one view. I want to populate each using a pickerview. Currently I have succesfully made one picker with one textfield working perfectly but once I edited the code to make it working with two textfield it does not working as expected. Any help will be much appreciated.

Below is my code :

#import "TestPickerVC.h"

@interface TestPickerVC () <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *aktivitiArray;
NSArray *penganjurArray;
}

@end

@implementation TestPickerVC

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

self.penganjurTextField.delegate = self;
self.aktivitiTextField.delegate = self;

aktivitiArray = @[@"apple", @"samsung", @"motorola", @"nokia"];
penganjurArray = @[@"jimbit", @"ayam", @"kambing", @"emergency"];


// self.aktivitiTextField.inputView = self.pickerView1;
// self.penganjurTextField.inputView = self.pickerView2;

self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;


self.aktivitiTextField.inputView = self.pickerView;

self.penganjurTextField.inputView = self.pickerView;

self.aktivitiTextField.tag = 1;
self.penganjurTextField.tag = 2;



// if (self.aktivitiTextField.tag == 100) {
// self.aktivitiTextField.inputView = self.pickerView;
// self.pickerView.tag = 1;
// NSLog(@"pickerview tag : %d", (int)self.pickerView.tag);
// } else if (self.penganjurTextField.tag == 200) {
// self.penganjurTextField.inputView = self.pickerView;
// self.pickerView2.tag = 2;
// NSLog(@"pickerview tag : %d", (int)self.pickerView2.tag);
//
// }

}

#pragma mark - UIPickerView DataSource

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

if (self.aktivitiTextField.tag == 1) {

return aktivitiArray.count;

} else if (self.penganjurTextField.tag == 2) {

return penganjurArray.count;
}

return 1;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (self.aktivitiTextField.tag == 1) {
return aktivitiArray[row];
} else if (self.penganjurTextField.tag == 2) {
return penganjurArray[row];
}

return @"";
}

#pragma mark - UIPickerView Delegate

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

NSString *selectedAktivitiRow = aktivitiArray[row];
NSString *selectedPenganjurRow = penganjurArray[row];


if (self.aktivitiTextField.tag == 1) {
self.aktivitiTextField.text = selectedAktivitiRow;
} else if (self.penganjurTextField.tag == 2) {
self.penganjurTextField.text = selectedPenganjurRow;
}

}




-(BOOL) textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];
return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end

Answer

Create another private variable as selectedTextField.

@interface TestPickerVC () <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSArray *aktivitiArray;
    NSArray *penganjurArray;
    int selectedTextField
}

The moment you tap on text field, textfield delegate method textFieldDidBeginEditing will be called. In this method, do -

selectedTextField = textField.tag;

Now in picker delegate methods, check with this condition :

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    if (selectedTextField == 1) {

        return aktivitiArray.count;

    } else if (selectedTextField == 2) {

        return penganjurArray.count;
    }

    return 1;

}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if (selectedTextField == 1) {
        return aktivitiArray[row];
    } else if (selectedTextField == 2) {
        return penganjurArray[row];
    }

    return @"";
}

Do likewise for all picker delegate methods.

Comments