2016-04-08 6 views
0

私はGMSAutocompleteFetcherを表示するgoogle place autocompleteを持っています。コードはGoogle developer websiteから来ていますが、問題は、テキストを表示しているユーザーがテキストを表示した直後に選択肢を処理する方法を見つけることができないことです。これは私のコードです:iOSでGMSAutocompleteFetcherを使用する場合の選択の処理方法は?

import UIKit 
    import GoogleMaps 

    class FetcherSampleViewController: UIViewController { 

     var textField: UITextField? 
     var resultText: UITextView? 
     var fetcher: GMSAutocompleteFetcher? 

     override func viewDidLoad() { 
     super.viewDidLoad() 
     let tap = UITapGestureRecognizer(target: self, action: "handletap:") 
     self.view.backgroundColor = UIColor.whiteColor() 
     self.edgesForExtendedLayout = .None 

     // Set bounds to inner-west Sydney Australia. 
     let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366, 
      longitude: 151.134002) 
     let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725, 
      longitude: 151.200349) 
     let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner, 
      coordinate: swBoundsCorner) 

     // Set up the autocomplete filter. 
     let filter = GMSAutocompleteFilter() 
     filter.type = .Establishment 

     // Create the fetcher. 
     fetcher = GMSAutocompleteFetcher(bounds: bounds, filter: filter) 
     fetcher?.delegate = self 

     textField = UITextField(frame: CGRect(x: 5.0, y: 0, 
      width: self.view.bounds.size.width - 5.0, height: 44.0)) 
     textField?.autoresizingMask = .FlexibleWidth 
     textField?.addTarget(self, action: "textFieldDidChange:", 
      forControlEvents: .EditingChanged) 

     resultText = UITextView(frame: CGRect(x: 0, y: 45.0, 
      width: self.view.bounds.size.width, 
      height: self.view.bounds.size.height - 45.0)) 
     resultText?.backgroundColor = UIColor(white: 0.95, alpha: 1.0) 
     resultText?.text = "No Results" 
     resultText?.editable = false 
     resultText?.addGestureRecognizer(tap) 
     self.view.addSubview(textField!) 
     self.view.addSubview(resultText!) 
     } 

     func textFieldDidChange(textField: UITextField) { 
     fetcher?.sourceTextHasChanged(textField.text!) 
     } 
     func handletap (sender: UITapGestureRecognizer){ 
     print("I dont know what to do here") 

     } 
    } 

    extension FetcherSampleViewController: GMSAutocompleteFetcherDelegate { 
     func didAutocompleteWithPredictions(predictions: [GMSAutocompletePrediction]) { 
    let resultsStr = NSMutableAttributedString() 
    for prediction in predictions { 
     resultsStr.appendAttributedString(prediction.attributedPrimaryText) 
     resultsStr.appendAttributedString(NSAttributedString(string: "\n")) 
    } 
    resultText?.attributedText = resultsStr 
} 

     func didFailAutocompleteWithError(error: NSError) { 
     resultText?.text = error.localizedDescription 
     } 
    } 

私はUITapGestureRecognizerを使用しますが、私は何をすべきか分かりません。あなたが私を助けることができればそれを感謝するでしょう:)

答えて

0

すべての予測はテキストフィールド内の改行で区切られた行だけなので、どのユーザがタップしたのかを判断するのは難しいでしょう。

UITextViewの代わりに、UITableViewを使用して、予測ごとに1つの行があります。これにより、どの予測が選択されたかを簡単に検出することができます。

関連する問題