2017-03-28 2 views
0

저녁에 SearchController, 나는 검색 컨트롤러를 구축하고, 또한 프로그래밍 그의 검색 창을 만들 수있는 코드가 있습니다. 하지만이 코드를 스토리 보드으로 디자인 된 검색 바로 바꾸고 싶습니다.아이폰 OS 스위프트 : 스토리 보드 UISearchBar

내 질문은 콘센트를 검색 컨트롤러에 어떻게 연결할 수 있습니까? 내가 검색 창에 입력 할 때, 결과가 테이블에

어떤을 보여주지 않기 때문에 나는 대표에 문제가 있다고 생각

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

하위 클래스 UISearchControllersearchBar getter를 재정 의하여 원하는 검색 바를 반환하십시오. 당신의 방법에서

public class mySearchController: UISearchController { 

    public var customSearchBar = UISearchBar() 
    override public var searchBar: UISearchBar { 
     get { 
      return customSearchBar 
     } 
    } 
} 

, 당신의 searchBarcustomSearchBar을 설정합니다.

func configureSearchController() {  
    searchController = mySearchController() 
    searchController.customSearchBar = self.searchBar 
    //Other stuff... 
} 
+0

팁 주셔서 감사합니다. 제발 좀 도와주세요. 나는 초보자입니다. –

+1

@ TheMiotz 지금 확인해보세요. –

+0

고마워요! 한 가지는 빠졌고, 이제 대리자를 통합하는 방법을 모르겠다. –

관련 문제