iOS Swift:使用StoryBoard的UISearchBar搜索控制器

3
晚上好,我已经构建了一个搜索控制器,并且我也有编程创建他的搜索栏的代码。但是我想用在storyboard中设计的搜索栏来替换这段代码。
所以我的问题是,我如何将outlet连接到搜索控制器?
以下是我的代码:
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))


    }

}

我认为代理出现了问题,因为当我在搜索栏中输入内容时,结果不会在表格中显示。

有什么建议吗?

1个回答

6

继承UISearchController并重写searchBar getter方法,以返回您想要的搜索栏。

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...
}

谢谢你的提示,你能给我一些代码吗?我是新手。 - Andrea Miotto
1
@TheMiotz 立即查看。 - Orkhan Alikhanov
1
我不明白你的意思,但是你可以像以前一样使用 searchController。只需执行 searchController.searchBar.delegate = self 即可。 - Orkhan Alikhanov
searchController.customSearchBar = self.customSearchBar 这一行是什么意思?请确保您分配了所需的正确 searchBar - Orkhan Alikhanov
让我们在聊天中继续这个讨论 - Andrea Miotto
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接