在Alamofire中,只调用UINavigationController!没有其他回应



我使用本教程来自动完成谷歌位置。

我已经通过cocoapad安装了alamofire和swiftyJSON。

我的问题是当我在搜索栏中输入文本时,什么都不会发生。

在我的ViewController:上

    let gpaViewController = GooglePlacesAutocomplete(
    apiKey: "MY-API-KEY",
    placeType: .Address
    )
    gpaViewController.placeDelegate = self
    presentViewController(gpaViewController, animated: true, completion: nil)

在我的GooglePlacesAutoComplete视图控制器:

import UIKit
import Alamofire
import SwiftyJSON
enum PlaceType: CustomStringConvertible {
case All
case Geocode
case Address
case Establishment
case Regions
case Cities
var description : String {
    switch self {
    case .All: return ""
    case .Geocode: return "geocode"
    case .Address: return "address"
    case .Establishment: return "establishment"
    case .Regions: return "regions"
    case .Cities: return "cities"
    }
}
}
struct Place {
    let id: String
    let description: String
}
protocol GooglePlacesAutocompleteDelegate {
    func placeSelected(place: Place)
    func placeViewClosed()
}    

只有这组代码被称为:

class GooglePlacesAutocomplete: UINavigationController {
var gpaViewController: GooglePlacesAutocompleteContainer?
var placeDelegate: GooglePlacesAutocompleteDelegate? {
    get { return gpaViewController?.delegate }
    set { gpaViewController?.delegate = newValue }
}
convenience init(apiKey: String, placeType: PlaceType = .All) {
    let gpaViewController = GooglePlacesAutocompleteContainer(
        apiKey: apiKey,
        placeType: placeType
    )
    self.init(rootViewController: gpaViewController)
    self.gpaViewController = gpaViewController
    let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: "close")
    gpaViewController.navigationItem.leftBarButtonItem = closeButton
    gpaViewController.navigationItem.title = "Enter Address"
}
func close() {
    placeDelegate?.placeViewClosed()
}
}

没有这方面的迹象:

class GooglePlaceSearchDisplayController: UISearchDisplayController {
override func setActive(visible: Bool, animated: Bool) {
    if active == visible { return }
    searchContentsController.navigationController?.navigationBarHidden = true
    super.setActive(visible, animated: animated)
    searchContentsController.navigationController?.navigationBarHidden = false
    if visible {
        searchBar.becomeFirstResponder()
    } else {
        searchBar.resignFirstResponder()
    }
}
}
// MARK: - GooglePlacesAutocompleteContainer
class GooglePlacesAutocompleteContainer: UIViewController {
    var delegate: GooglePlacesAutocompleteDelegate?
    var apiKey: String?
    var places = [Place]()
    var placeType: PlaceType = .All
convenience init(apiKey: String, placeType: PlaceType = .All) {
    self.init(nibName: "GooglePlacesAutocomplete", bundle: nil)
    self.apiKey = apiKey
    self.placeType = placeType
}
override func viewDidLoad() {
    super.viewDidLoad()
    let tv: UITableView? = searchDisplayController?.searchResultsTableView
    tv?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
}

或者这个:

// MARK: - GooglePlacesAutocompleteContainer (UITableViewDataSource / UITableViewDelegate)
extension GooglePlacesAutocompleteContainer: UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return places.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.searchDisplayController?.searchResultsTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell?
    // Get the corresponding candy from our candies array
    let place = self.places[indexPath.row]
    // Configure the cell
    cell!.textLabel!.text = place.description
    cell!.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    delegate?.placeSelected(self.places[indexPath.row])
}
}
// MARK: - GooglePlacesAutocompleteContainer (UISearchDisplayDelegate)
 extension GooglePlacesAutocompleteContainer: UISearchDisplayDelegate {
    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
    getPlaces(searchString)
    return false
}
private func getPlaces(searchString: String) {
    let url = "https://maps.googleapis.com/maps/api/place/autocomplete/json"
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success(let data):
            let json = JSON(data)
            let name = json["name"].stringValue
            print(name)
        case .Failure(let error):
            print("Request failed with error: (error)")
        }
    }
}
}

抱歉代码太长。不确定,我在哪里犯了错误。我还在xib中设置了searchBar的委托我仔细检查了Cocoapads、Alamofire和SwiftyJson的安装情况。有人能帮帮我吗?我对这些东西还不熟悉。。

您必须将UISearchBar的对象做成这样

lazy var searchBar : UISearchBar = UISearchBar()

实际加载

searchBar.delegate = self

最新更新