菜鸟教程小白 发表于 2022-12-13 12:11:37

ios - 如何在一个 View 中将一个 UIPickerView 用于两个文本字段?


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

<p>下面是我的代码:</p>

<pre><code>#import &#34;TestPickerVC.h&#34;

@interface TestPickerVC () &lt;UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate&gt;
{
    NSArray *aktivitiArray;
    NSArray *penganjurArray;
}

@end

@implementation TestPickerVC

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

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

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


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

    self.pickerView = [ 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(@&#34;pickerview tag : %d&#34;, (int)self.pickerView.tag);
//    } else if (self.penganjurTextField.tag == 200) {
//      self.penganjurTextField.inputView = self.pickerView;
//      self.pickerView2.tag = 2;
//      NSLog(@&#34;pickerview tag : %d&#34;, (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;
    } else if (self.penganjurTextField.tag == 2) {
      return penganjurArray;
    }

    return @&#34;&#34;;
}

#pragma mark - UIPickerView Delegate

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

    NSString *selectedAktivitiRow = aktivitiArray;
    NSString *selectedPenganjurRow = penganjurArray;


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

}




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

    ;
    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 .
// Pass the selected object to the new view controller.
}
*/

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>创建另一个私有(private)变量作为 selectedTextField。</p>

<pre><code>@interface TestPickerVC () &lt;UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate&gt;
{
    NSArray *aktivitiArray;
    NSArray *penganjurArray;
    int selectedTextField
}
</code></pre>

<p>点击文本字段的那一刻,文本字段委托(delegate)方法 <code>textFieldDidBeginEditing</code> 将被调用。在这个方法中,做 - </p>

<pre><code>selectedTextField = textField.tag;
</code></pre>

<p>现在在选择器委托(delegate)方法中,检查这个条件:</p>

<pre><code>- (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;
    } else if (selectedTextField == 2) {
      return penganjurArray;
    }

    return @&#34;&#34;;
}
</code></pre>

<p>对所有选择器委托(delegate)方法都这样做。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在一个 View 中将一个 UIPickerView 用于两个文本字段?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/34022967/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/34022967/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在一个 View 中将一个 UIPickerView 用于两个文本字段?