如何使用两个文本字段 ios 的自动完成功能?

How to use autocomplete with two textfields ios?

在我的应用程序中,我有两个 textFields,通过单击每个 textField,我可以从 google 个地方 autocomplete.In 用户加载 select 个地方处理程序我必须分配我必须在哪个文本字段中显示 result.If 我单击第一个文本字段,值显示第二个文本字段副 versa.I 为两个文本字段设置标签值。下面是我提前完整的code.Thanks。

//when first textfield clicked
- (IBAction)onLaunchClicked:(id)sender {
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];

    }

//when second textfield clicked
- (IBAction)to_click:(id)sender {
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];
    }


// Handle the user's selection.
    - (void)viewController:(GMSAutocompleteViewController *)viewController
    didAutocompleteWithPlace:(GMSPlace *)place {
        [self dismissViewControllerAnimated:YES completion:nil];
        // Do something with the selected place.
        NSLog(@"Place name %@", place.name);
        NSLog(@"Place address %@", place.formattedAddress);
        NSLog(@"Place attributions %@", place.attributions.string);

 //have to set values in correct textfields
    if (textfield.tag == 10001){
    from_txt.text=place.formattedAddress;
   }
    else {
    to_txt.text= place.formattedAddress;
        }

}

您需要在 viewController 中再声明一个实例 textField 像这样

UITextField *selTextField;

现在像这样

在你的IBAction中将点击的文本字段的引用设置为 selTextField
- (IBAction)onLaunchClicked:(id)sender {
    self.selTextField = (UITextField*) sender;
    GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
    acController.delegate = self;
    [self presentViewController:acController animated:YES completion:nil];
}

- (IBAction)to_click:(id)sender {
    self.selTextField = (UITextField*) sender;
    GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
    acController.delegate = self;
    [self presentViewController:acController animated:YES completion:nil];
}

现在在 GMSAutocompleteViewController

的委托方法中
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
    [self dismissViewControllerAnimated:YES completion:nil];
    // Do something with the selected place.
    NSLog(@"Place name %@", place.name);
    NSLog(@"Place address %@", place.formattedAddress);
    NSLog(@"Place attributions %@", place.attributions.string);

    //Set textField value
    self.selTextField.text=place.formattedAddress;
}