作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
晚上,我构建了一个搜索 Controller ,而且我还有以编程方式创建他的搜索栏的代码。但我想用 Storyboard中设计的搜索栏替换此代码。
所以我的问题是,如何将 socket 连接到搜索 Controller ?
这是我的代码:
public class CustomSearchController: UISearchController {
public var customSearchBar = UISearchBar()
override public var searchBar: UISearchBar {
get {
return self.customSearchBar
}
}
}
func configureSearchController() {
searchController = CustomSearchController()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.customSearchBar = self.customSearchBar
searchController.searchBar.delegate = self
self.definesPresentationContext = true
}
extension EarthSearcherTableViewController : UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
//Code
guard let text = searchController.searchBar.text else { return }
self.getLocations(forSearchString: text)
}
fileprivate func getLocations(forSearchString searchString: String) {
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchString
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { (response, error) in
guard let response = response else { return }
self.locations = response.mapItems
self.tableView.reloadData()
}
}
@objc func zoomToCurrentLocation() {
//Clear existing pins
mapView.removeAnnotations(mapView.annotations)
mapView.removeOverlays(mapView.overlays)
let annotation = MKPointAnnotation()
annotation.coordinate = mapView.userLocation.coordinate
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.005, 0.005)
let region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span)
mapView.setRegion(region, animated: true)
let location = CLLocation(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
mapView.add(MKCircle(center: location.coordinate, radius: 50))
}
}
我认为我对代表有问题,因为当我在搜索栏中输入时,结果不会在表格中显示
有什么建议吗?
最佳答案
子类 UISearchController
并覆盖 searchBar
getter 以返回您想要的 searchBar。
public class mySearchController: UISearchController {
public var customSearchBar = UISearchBar()
override public var searchBar: UISearchBar {
get {
return customSearchBar
}
}
}
在您的方法中,将 customSearchBar
设置为您的 searchBar
。
func configureSearchController() {
searchController = mySearchController()
searchController.customSearchBar = self.searchBar
//Other stuff...
}
关于iOS swift : SearchController with a StoryBoard UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070433/
我是一名优秀的程序员,十分优秀!