如何在一个视图中使用一个UIPickerView用于两个文本字段?

时间:2015-12-01 14:37:27

标签: ios uipickerview

我在一个视图中有2个文本字段。我想使用pickerview填充每个。目前我已经成功地制作了一个带有一个文本字段的选择器,但是一旦我编辑了代码以使其与两个文本字段一起工作,它就无法按预期工作。任何帮助将不胜感激。

以下是我的代码:

#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

4 个答案:

答案 0 :(得分:2)

创建另一个私有变量作为selectedTextField。

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

点击文本字段时,将调用文本字段委派方法textFieldDidBeginEditing。在这种方法中,做 -

selectedTextField = textField.tag;

现在在picker委托方法中,请检查以下条件:

- (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 @"";
}

同样适用于所有选择器委托方法。

答案 1 :(得分: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;
}

这里的问题是第一个if语句将始终为真。无论选择器用于哪个文本字段,self.aktivitiTextField.tag == 1始终为真。您在所有选择器视图方法中都遇到此问题。

您需要检查当前使用的文本字段。在所有选择器视图方法中尝试以下模式:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([self.aktivitiTextField isFirstResponder]) {
        return aktivitiArray.count;
    } else if ([self.penganjurTextField isFirstResponder]) {
        return penganjurArray.count;
    }

    return 1;
}

答案 2 :(得分:1)

相反,您可以将.editing用于文本字段并返回相应的数组。 例如:

if (aktivitiTextField.editing) {

        return aktivitiArray.count;

    } else if (penganjurArray.editing) {

        return penganjurArray.count;
    }

答案 3 :(得分:0)

在swift 3中,这对我有用。以下是代码。

//MARK:- picker view methods


func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if (textFieldName .isEqual(to: "firstDoseFld")) {
        return arrFirstDose.count

    }
    else
    {
        return arrFrequency.count
    }



}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    if (textFieldName .isEqual(to: "firstDoseFld")) {
        return arrFirstDose[row]
    } else  {
        return arrFrequency[row]
    }




}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if (textFieldName .isEqual(to: "firstDoseFld"))
    {
        selectedFirstDose = NSString()
        selectedFirstDose = arrFirstDose[row] as NSString!
        let cell : AddMedicationCell = ordersTable.cellForRow(at: indexPathForCell as IndexPath) as! AddMedicationCell

        cell.firstDoseFld.text = selectedFirstDose as String?

    } else
    {
        selectedFrequency = NSString()
        selectedFrequency = arrFrequency[row] as NSString!
        let cell : AddMedicationCell = ordersTable.cellForRow(at: indexPathForCell as IndexPath) as! AddMedicationCell

        cell.frequencyField.text = selectedFrequency as String?

    }


}

在textfield委托方法中这样做

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
{



    print("textfield is ..\(textField)")

    if textField.placeholder == "First Dose"
    {
        textFieldName = "firstDoseFld"
    }
    else if textField.placeholder == "Frequency"
    {
        textFieldName = "frequencyFld"
    }

    activeTextfield = textField

    print(selectedTextField)
    return true;
}

在cellForRowAtIndexPath中这样做(设置自定义单元格文本字段的委托)并在此处添加工具栏

orderCell.firstDoseFld.delegate = self
    orderCell.frequencyField.delegate = self

let toolBar4 = UIToolbar()
        toolBar4.barStyle = .default
        toolBar4.isTranslucent = true
        toolBar4.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
        toolBar4.sizeToFit()
        let doneButtonForFirstDoseFld = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(AddMedicationOrderVC.doneClickedForFirstDoseFld))
        let spaceButton4 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        // let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: "cancelClick")
        toolBar4.setItems([spaceButton4, doneButtonForFirstDoseFld], animated: false)
        toolBar4.isUserInteractionEnabled = true

        orderCell.firstDoseFld.inputView = pickerView
        orderCell.firstDoseFld.inputAccessoryView = toolBar4
        orderCell.firstDoseFld.tintColor = UIColor.clear

如果您需要更多信息,请与我们联系。 Delare变量基于对此的要求。