15

このような質問はたくさんありますが、私のケースはユニークだと思います。

テキストボックス内をクリックするとポップアップするピッカービューがあります。ユーザーがピッカービューで自分の場所を選択すると、それがテキスト ボックスに入力されます。

ピッカービューを開くと、スライダーは最初の要素にありますが、完了ボタンをクリックしてピッカービューを最小化すると、その要素は選択されません (つまり、最初の要素を選択するには、下にスクロールして戻る必要があります)

私はもう試した

    [pickerView selectRow:0 inComponent:0 animated:YES];

およびそれのさまざまな組み合わせですが、スライダーをその要素に配置するだけで、実際に選択することはありません。

上記のコードを viewDidLoad() メソッドに適用するという回答もありましたが、残念ながら、Web サービスから位置配列を取得しているため、それを行うことはできません。

理想的には、ユーザーがテキストボックスをクリックすると、ピッカービューが通常どおりポップアップし、その時点で完了ボタンをクリックすると、最初の項目が選択されます。

前もって感謝します

4

5 に答える 5

14

スイフト 3+

func textFieldDidBeginEditing(_ textField: UITextField) {
    if textField == yourTextField{
        self.yourPickerView.selectRow(0, inComponent: 0, animated: true)
        self.pickerView(yourPickerView, didSelectRow: 0, inComponent: 0)
    }
}

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

//Do all the stuff to populate the TextField

}
于 2016-10-03T18:51:41.627 に答える
5

これがあなたが試すことができるものです:

@interfaceのような変数を設定しますNSString *selectedString;

UIPickerView(in )を初期化するときpickerView:titleForRow:forComponent:、行0のタイトルを設定するときselectedStringは、行#0のタイトルと同じ文字列を設定します。次に、で、適切な文字列をpickerView:didSelectRow:inComponent:設定します。selectedString

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

    // Some stuff

    // Assuming "yourDataArray" contains your strings
    selectedString = [yourDataArray objectAtIndex:row];

}

次に、「完了」ボタンを押すことによってトリガーされるメソッドを呼び出すときに、を使用selectedStringしてテキストを設定します。

これがあなたのためにうまくいくことを願っています!

于 2013-01-30T13:42:26.850 に答える
4

私は同じ問題を抱えていました。私の解決策はこれでした:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Only for the text field with the picker
    if (textField == self.yourTextField)
    {
        // Select the first row programmatically 
        [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
        // The delegate method isn't called if the row is selected programmatically
        [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
    }



}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //Do all the stuff to populate the TextField

}
于 2014-12-04T15:35:56.173 に答える
3
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Only for the text field with the picker
    if (textField == self.yourTextField && [textField length]!=0)
    {
        // Select the first row programmatically 
        [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
        // The delegate method isn't called if the row is selected programmatically
        [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
    }



}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //Do all the stuff to populate the TextField
}
于 2015-02-03T11:55:31.657 に答える
0

/イベント編集が開始されたテキストフィールドの IBACTION を作成し、関数はこのようにフィールドが空かどうかをチェックする必要があります/

-(IBAction)asignFirst:(UITextField *)sender{
if ([sender.text isEqualToString:@""])
{
    [yourPicker selectRow:0 inComponent:0 animated:YES]; 
    sender.text = self.pickerData[0];
   }
}
于 2014-11-21T20:41:14.090 に答える