您好,我在FirstVC中有一个按钮,在SecondVC中我在navigationBar上有一个搜索框,它是Google自动填充搜索栏,用户在其中输入内容并显示地点。我想要的是当用户点击按钮并且第二个VC来时,一旦用户选择了该位置,第一个VC应该显示在secondVC上选择的数据而无需用户按下任何操作按钮。
在实施Google自动填充功能之前,我在TableView中显示数据库中的位置,我通过在didSelectRowAtIndexPath
中编写一些代码将用户重定向到第一个控制器,一切正常。它不在这里工作。我会把我的代码粘贴到我现在正在做的事情
FirstVC:
class AddRequestTableViewController:GooglePlacesViewControllerDelegate{
func country(place: GMSPlace!) {
dismissKeyboard()
print("Place name: ", place.name)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let destination = segue.destinationViewController as! GooglePlacesViewController
destination.delegate = self
}
}
SecondVC
protocol GooglePlacesViewControllerDelegate {
func country(place: GMSPlace!,departureOrArrivalSegue:Int)
}
class GooglePlacesViewController: UIViewController {
var delegate : GooglePlacesViewControllerDelegate! = nil
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
var locationManager = CLLocationManager()
var placesClient : GMSPlacesClient?
var departureOrArrivalSegue:Int?
override func viewDidLoad() {
super.viewDidLoad()
//locationManager.startUpdatingLocation()
// locationManager = CLLocationManager()
// locationManager.delegate = self
//locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
//locationManager.startUpdatingLocation()
//locationManager.startMonitoringSignificantLocationChanges()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
// Put the search bar in the navigation bar.
searchController?.searchBar.sizeToFit()
self.navigationItem.titleView = searchController?.searchBar
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
self.definesPresentationContext = true
// Prevent the navigation bar from being hidden when searching.
searchController?.hidesNavigationBarDuringPresentation = false
let camera = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.leftBarButtonItem = camera
placesClient = GMSPlacesClient()
run()
}
func run(){
placesClient?.currentPlaceWithCallback({
(placeLikelihoodList: GMSPlaceLikelihoodList?, error: NSError?) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let placeLicklihoodList = placeLikelihoodList {
let place = placeLicklihoodList.likelihoods.first! as GMSPlaceLikelihood
if let place : GMSPlaceLikelihood = place {
print("place is \(place.place.formattedAddress)")
}
}
})
}
}
// Handle the user's selection.
extension GooglePlacesViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController!,
didAutocompleteWithPlace place: GMSPlace!) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
print("cordinates are \(place.coordinate)")
delegate.country(place)
}
func resultsController(resultsController: GMSAutocompleteResultsViewController!,
didFailAutocompleteWithError error: NSError!){
// TODO: handle the error.
print("Error: ", error.description)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController!) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController!) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
答案 0 :(得分:0)
您的委托方法是:
func country(place: GMSPlace!,departureOrArrivalSegue:Int)
但你只打电话:
delgate.country(place)
您是否收到编译错误?您的委托调用应该有另一个参数。
您的GooglePlacesViewController'尚未声明自己是' resultsViewController'的代表。你打电话给:
resultsViewController?.delegate = self
所以你的GooglePlacesViewController'应该看起来像这样:
class GooglePlacesViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate {
你如何接收编译器错误超出了我的范围。你在使用nano吗?