草庐IT

iOS swift : SearchController with a StoryBoard UISearchBar

coder 2023-09-09 原文

晚上,我构建了一个搜索 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/

有关iOS swift : SearchController with a StoryBoard UISearchBar的更多相关文章

  1. ios - 如何自定义 searchBar 范围 - 2

    所以我一直在到处寻找,但无法就此找到答案。我有一个UISearchBarsearchBar设置来过滤我的数据。我遇到了范围功能,但创建了我自己的范围按钮。所以我的问题是,如何链接我制作的那些按钮,以便它们可以执行与UISearchBar范围相同的操作? 最佳答案 范围按钮比其他任何东西都更方便。它们不会自动更改搜索范围。这是您在响应用户更改范围时必须做的事情。通常,您不会自己制作按钮。您在搜索栏的scopeButtonTitles属性中设置按钮的标题,例如:self.searchController.searchBar.scopeB

  2. iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar - 2

    我正在尝试让新的iOS-11searchBar方法起作用,但没有成功。searchBar出现了,但显示在左栏按钮下方,而不是旁边:在横屏模式下也是如此。我使用的代码是:self.searchResults=[[SearchResultsTablealloc]init];self.searchResults.delegate=self;searchController=[[UISearchControlleralloc]initWithSearchResultsController:self.searchResults];searchController.searchResultsUpd

  3. iOS - ScopeBar 与 TabBarController 中的 UISearchController 中的 SearchBar 重叠 - 2

    我遇到了一个关于在我的UISearchBar下显示的范围栏的特殊问题。基本上,我以前遇到的问题是,每当我的UISearchController处于事件状态并且用户切换选项卡时,如果他回到包含UISearchController的UIViewController,背景就会返回。通过将UIViewController嵌入UINavigationController解决了这个问题。现在,出现了一个新问题。当我在UISearchController已经激活的情况下切换选项卡时,当我切换回来时,UIScopeBar显示在UISearchBar的顶部。这只能通过取消搜索并重新开始来解决。插图:我试

  4. ios - 如何使用SearchBar从服务器获取数据 - 2

    我有一个由TextField、Button和TableView组成的设计。用户在TextField中输入文本,然后单击启动搜索的按钮。当结果从服务器返回时,结果显示在TableView中。我想使用SearchBar,而不是TextField和Button。我不清楚该怎么做。我能找到关于UISearchBar和UISearchDisplayController的每个教程和示例,就好像两者是密不可分的。我只想使用SearchBar启动Internet搜索—与TableView完全没有连接:我通过TableView显示数据只是偶然的。那么如何从“TextFieldandButton”切换到S

  5. iOS 8 UISearchController 的 searchBar 重叠 tableVIew - 2

    我正在练习使用iOS8的新功能-UISearchController来显示我的tableView和结果。但是奇怪的事情发生了。看起来searchBar是透明的。是的,searchBar与tableView重叠。我在SO中搜索了很多,但没有帮助。我在viewDidLoad中的实现self.myTableView=[[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];_myTableView.delegate=self;_myTableView.dataSource=self;[self.

  6. ios - 关闭 SearchBar 上的自动建议 - 2

    我正在使用SearchBar,在输入至少三个字符后,我会显示一个TableView列表,其中包含字典中的可能单词。用户可以点击此列表中的单词以选择所需的单词。只要iOS不显示他自己的建议(在输入字段下方以蓝色显示),这就可以正常工作。示例:我输入“autof”,TableView显示两个条目(德语1.“Autofahren”和2.“Autofahrer”)。iOS以蓝色显示“自动对焦”一词,并用“X”将其删除。如果我在这个状态下选择第二个条目,我想搜索“Autofahrer”,但是委托(delegate)方法告诉我,搜索词已经变成了“autofocus”。现在,在处理TableView

  7. iphone - xib 中带有 customcell 的 UItableview 和 searchbar - 2

    我是Iphone应用程序开发的新手。我需要开发一个带有UISearchbar的UITableview。我必须在tableView中显示一个json数组,默​​认为一个image和两个buttons和两个标签。两个按钮的功能是+和-一个标签用于显示按钮操作,一个标签用于显示json数组名称的数字。确切地说,我需要显示的是...在特定单元格的标签中单击按钮的次数,并在搜索栏中搜索相同项目时显示相同的次数。谁能帮帮我。请....提前致谢。这是我试过的代码..-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtInde

  8. ios - 如何让 iOS SearchBar 键盘搜索按钮执行操作 - 2

    我有一个简单的应用程序,它有一个TableView(TV1),用户选择一个导航栏按钮项目以添加条目(VC1)来填充它。在添加条目中,当用户按下名称字段时,他们将被带到另一个TableView(TV2),该View用于允许用户选择以前输入的名称或创建新名称。TV2是一个TableViewController,顶部有一个搜索栏。现在,现有名称可用并显示给用户。但是,如果用户“搜索”一个不存在的名称,他们应该可以选择立即创建该名称。如果不存在搜索,我有一个按钮,用户可以点击该按钮来创建用户。很简单。然而,这是另一个水龙头。有没有办法让键盘的搜索按钮执行创建按钮的相同功能(即保存新用户并关闭T

  9. ios - UISearchController:searchBar 和 scopeBar 在第一次触摸事件时重叠 - 2

    我正在尝试使用Apple最新的UISearchController实现一个简单的搜索栏。但是,如果我使用搜索栏的范围栏来获取过滤器选项,我似乎无法使其正常工作.范围栏总是显示我可以接受但是在第一次触摸事件中,搜索栏和范围栏重叠。我使用了Apple的TableView示例代码应用程序,但它没有任何改变。-(void)viewDidLoad{[superviewDidLoad];_resultsTableController=[[APLResultsTableControlleralloc]init];_searchController=[[UISearchControlleralloc]

  10. ios - 推送 View Controller 时,UISearchController searchBar 不会消失 - 2

    我在maUIViewcontroller中使用UISearchController,其中包含一个UITableView,我在viewDidLoad中执行此操作:self.searchController=[[UISearchControlleralloc]initWithSearchResultsController:nil];self.searchController.delegate=self;self.searchController.searchResultsUpdater=self;self.searchController.searchBar.delegate=self;s

随机推荐