ios - 两个文本字段显示相同的 GMSPlace 自动完成

标签 ios swift google-maps autocomplete uitextfield

我是我的 GMSAUtocompleteController,我有两个文本字段,但单击时它们显示相同的位置

extension RideAddDetailsViewController: GMSAutocompleteViewControllerDelegate {
    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {


    locationTextField.text = place.name
    destinationTextField.text = place.name


    dismiss(animated: true, completion: nil)
  }
   func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    // Handle the error
    print("Error: ", error.localizedDescription)
 }
 func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    // Dismiss when the user canceled the action
    dismiss(animated: true, completion: nil)
}

最佳答案

可以使用标签来分隔它们。

    if textField.isEqual(locationTextField)
    {

       let autocompleteController = GMSAutocompleteViewController()
       autocompleteController.view.tag = 1 // assign the tag you want
       autocompleteController.delegate = self

       present(autocompleteController, animated: true, completion: nil)
    }
    else if  textField.isEqual(destinationTextField)
   {

       let autocompleteController = GMSAutocompleteViewController()
       autocompleteController.view.tag = 2 // assign the tag you want
       autocompleteController.delegate = self

       present(autocompleteController, animated: true, completion: nil)
    }

现在您可以像这样将值与委托(delegate)方法分开并分配给文本字段。

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {

    if viewController.view.tag == 1
    {
       locationTextField.text = place.name
     }
     else viewController.view.tag == 2
     {
        destinationTextField.text = place.name
     }


        dismiss(animated: true, completion: nil)
  }

关于ios - 两个文本字段显示相同的 GMSPlace 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56376731/

相关文章:

ios - PushViewController 无法正常工作

ios - UITableViewCell : UITapGestureRecognizer unrecognized selector sent to instance error

ios - 将文件从 iOS 11 Files 应用程序复制到沙箱

iphone - 获取 Facebook 应用程序订阅者

ios - 将字符串作为标签或按钮中的其他值传递

ios - 从 URL 下载图片 iOS 10 Swift 3

swift - 在使用 swift 展开可选值蓝牙时意外发现 nil

java - 单击标记android时如何打开html弹出窗口

android - AutocompleteFragment 结果返回一个具有 null 属性的地方

javascript - 使用 Javascript,我如何随机偏移经度和纬度数字