在 Alamofire 中,只有 UINavigationController 被调用!没有其他反应
Posted
技术标签:
【中文标题】在 Alamofire 中,只有 UINavigationController 被调用!没有其他反应【英文标题】:In Alamofire only the UINavigationController is called! no other response 【发布时间】:2015-12-16 04:42:47 【问题描述】:我正在使用this tutorial 自动完成谷歌位置。
我已经通过 cocoapads 安装了 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 的安装。有人可以帮帮我吗?我对这些东西不熟悉..
【问题讨论】:
你设置你的 UISearchDisplayController delegate = self 了吗? 是的,我将代理设置为 self。但它不起作用。 【参考方案1】:你必须像这样制作 UISearchBar 的对象...
lazy var searchBar : UISearchBar = UISearchBar()
加载中
searchBar.delegate = self
【讨论】:
以上是关于在 Alamofire 中,只有 UINavigationController 被调用!没有其他反应的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 3.0 - iOS 9.1 上的 EXC_BAD_ACCESS