gpt4 book ai didi

swift - 如何在 Google Place AutocompleteViewController 的 GMSAutocompleteViewController 中设置文本

转载 作者:IT王子 更新时间:2023-10-29 05:46:05 26 4
gpt4 key购买 nike

在我的应用程序中打开时,我需要帮助在 GMSAutocompleteViewController 的搜索栏中设置文本。我在 Google Place AutocompleteViewController 中使用 GMSAutocompleteViewController .

enter image description here

最佳答案

通过结合@Youngjin 和@Exception 对 Swift 4 和 Google Places 2.6.0 的回答,我得到了一个可行的解决方案

访问 GMSAutocompleteViewController 搜索栏:

let views = gmsAutoCompleteViewController.view.subviews
let subviewsOfSubview = views.first!.subviews
let subOfNavTransitionView = subviewsOfSubview[1].subviews
let subOfContentView = subOfNavTransitionView[2].subviews
let searchBar = subOfContentView[0] as! UISearchBar

然后设置文本并自动搜索:

searchBar.text = "Your address"
searchBar.delegate?.searchBar?(searchBar, textDidChange: "Your address") // This performs the automatic searching.

我发现在尝试从内部设置文本时收到 EXC_BAD_ACCESS 错误didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController).

所以我改为将这段代码放在我展示 autoCompleteController 的完成 block 中,它可以正常工作。

将它们组合在一起:

let gmsAutoCompleteViewController = GMSAutocompleteViewController()
gmsAutoCompleteViewController.delegate = self

present(gmsAutoCompleteViewController, animated: true) {
let views = gmsAutoCompleteViewController.view.subviews
let subviewsOfSubview = views.first!.subviews
let subOfNavTransitionView = subviewsOfSubview[1].subviews
let subOfContentView = subOfNavTransitionView[2].subviews
let searchBar = subOfContentView[0] as! UISearchBar
searchBar.text = "Your address"
searchBar.delegate?.searchBar?(searchBar, textDidChange: "Your address")
}

编辑:我发现这个解决方案似乎只适用于 iOS 11。让 searchBar = subOfContentView[0] 为! UISearchBar将在 iOS 10 和可能更低的版本上失败。

关于swift - 如何在 Google Place AutocompleteViewController 的 GMSAutocompleteViewController 中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41381233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com