gpt4 book ai didi

swift - 点击 Google 地方信息中的选择后显示 View Controller 的更好方法

转载 作者:行者123 更新时间:2023-11-30 11:52:53 24 4
gpt4 key购买 nike

我正在按照教程在我的应用程序中使用 Google 地方信息,我注意到点击过滤结果后,Google 地方信息 View Controller 会被关闭,并显示调用 Google 地方信息的上一个屏幕。我不知道如何调整它,以便用户点击结果后它会直接转到另一个 Controller 。现在,我在点击结果后,Google Places Controller 被关闭,我调用 present 来调出另一个 Controller 。用户点击结果后,如何转到下一个 Controller 而不返回到调用 Google 地方信息的 Controller ?

import UIKit
import GooglePlaces

class TravelerViewController: UIViewController {

@IBAction func autocompleteClicked(_ sender: UIButton) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}

extension TravelerViewController: GMSAutocompleteViewControllerDelegate {

// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TravelerResults")

dismiss(animated: true, completion: nil)
self.present(controller, animated: true, completion: nil)
}

func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}

// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}

// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

}

最佳答案

为打开的 View Controller 创建单独的函数。

@objc func openVC() {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "TravelerResults")
self.present(controller!, animated: true, completion: nil)
}

在自动完成中调用此函数。

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress)")
print("Place attributions: \(place.attributions)")
dismiss(animated: true, completion: nil)
openVC()

}

关于swift - 点击 Google 地方信息中的选择后显示 View Controller 的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155972/

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