简体   繁体   English

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

[英]In Alamofire only the UINavigationController is called! no other response

I am using this tutorial to auto-complete the google places. 我正在使用本教程自动完成Google地方信息。

I have installed the alamofire and swiftyJSON through cocoapads. 我已经通过cocoapads安装了alamofire和swiftyJSON。

My issue is When I enter text in searchbar, nothing happens. 我的问题是,当我在搜索栏中输入文字时,什么也没发生。

On my ViewControlller: 在我的ViewControlller上:

    let gpaViewController = GooglePlacesAutocomplete(
    apiKey: "MY-API-KEY",
    placeType: .Address
    )

    gpaViewController.placeDelegate = self

    presentViewController(gpaViewController, animated: true, completion: nil)

On my GooglePlacesAutoComplete View Controller: 在我的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()
}    

Only this set of codes is called: 仅将此代码集称为:

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()
}

}

No indication of this: 没有迹象表明:

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")

}

}

Or this: 或这个:

// 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)")
        }
    }
}
}

Apologies for long code. 对长代码表示歉意。 Am not sure, where I made a mistake. 不确定,我在哪里弄错了。 I also set the delegate for searchBar in xib I double checked the installation of Cocoapads, Alamofire and SwiftyJson. 我还在searchBar中为searchBar设置了searchBar ,我仔细检查了Cocoapads,Alamofire和SwiftyJson的安装。 Can someone please help me? 有人可以帮帮我吗? I'm new to these things.. 我是这些东西的新手。

You Have To Make Object Of UISearchBar Like This... 您必须像这样使UISearchBar对象...

lazy var searchBar : UISearchBar = UISearchBar()

In Did Load 中载

searchBar.delegate = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM