Swift Search Result Controller in search results segue to another view controller

淺唱寂寞╮ 提交于 2020-01-13 05:27:29

问题


Problem:

I have a table view that the user can either scroll through to find something or use a search bar. The search bar was not created using the StoryBoard. My view has a UISearchController that handles the search bar and search result updating. The issue that I'm running into is that since my SearchResultsController is instantiated by another controller I cannot perform a segue to another view, or at least I can't find the solution. Everything works except for the segue between my Search Results Controller and the view it's destined for.

What I would like to do

Have my MyNavTableViewController delegate for MySearchResultsController. In the search results controller it will segue to another view when the user taps on a TableViewCell. I'm not using the StoryBoard to accomplish this I'm having difficulty using segues to transition to another view.

If I can't get this to work what I will probably do:

It's essential that I pass information between views, and for me I've always done it using segues. However if this doesn't work I will probably try presenting a view modally by pushing it unto the navigation controller stack and write the data to a shared database or something. I would prefer to use a segue though.

Research: There is definitely more than this, but I'm not going to take too much space on urls.

Creating a segue programmatically

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html

My Setup

I'm going to try and keep this as concise as possible. There is more code than what I'm displaying. I'm just going to try and clean it up so that I'm only presenting the important stuff. I'm also changing a few names around because there may be sensitive information. It's probably not a big deal, but I'd rather be safe.

class MyNavTableViewController: UIViewController, UITableViewDataSource{

    //this is 
    @IBOutlet weak var tableView: UITableView!
    var searchController: UISearchController!


    override func viewDidLoad(){
      ...code 

         tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: tblId)

    let resultsController = MySearchResultsController()
    resultsController.databaseFilePath = databaseFilePath()
    //this is essential that I use a segue because between my views I'm passing information between them.
    resultsController.photo = photo!

    searchController = UISearchController(searchResultsController:       resultsController)
    let searchBar = searchController.searchBar
    searchBar.placeholder = searchBarPlaceHolderText
    searchBar.sizeToFit()
    tableView.tableHeaderView = searchBar
    searchController.searchResultsUpdater = resultsController

    }


}


MySearchResultsController: UITableViewController, UISearchResultsUpdating {

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    //self.performSegueWithIdentifier(imagePrepareStoryBoardId, sender: tableView.cellForRowAtIndexPath(indexPath))
    /*let imagePrepare = ImagePrepareController()
    customSegue = SearchResultSegue(identifier: imagePrepareId, source: self, destination: imagePrepare)*/

    //neither storyboard nor navigationController can be nil.
    let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)
    //destVC.photo = photo!


    //self.presentViewController(destVC, animated: false, completion: nil)

    self.navigationController!.pushViewController(destVC, animated: false)


    }
}

My Failed Attempts

1) Straight up segue - Doesn't work since the MySearchResultsController is not a view in the storyboard. Everything from what I've read is that segues can only be created in the SB.

2) Push view onto the navigation stack. The only problem with this is that I can't send data between views (or at least from what I've read). I'm also getting this error right at this break point:

let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)

fatal error: unexpectedly found nil while unwrapping an Optional value

I double checked the imagePrepareStoryBoardId. It's correct.

3) Use custom segue - As you can see from the commented out lines that this isn't working either. I've always used segues with the SB so this method is a little new to me. I might be messing it up somewhere.


回答1:


First create a protocol

protocol SelectedCellProtocol {
    func didSelectedCell(text: String)
}

on your UITableViewClass declare it

class MySearchResultsController: UITableViewController, UISearchResultsUpdating {
    var delegate:SelectedCellProtocol?
}

and on the selected cell method call it like :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    self.delegate?.didSelectedCell(cell.textLabel?.text)
}

when you declare your results controller, set the delegate

let resultsController = MySearchResultsController()
    resultsController.databaseFilePath = databaseFilePath()
    //this is essential that I use a segue because between my views I'm passing information between them.
    resultsController.photo = photo!
resultsController.delegate = self

    searchController = UISearchController(searchResultsController:       resultsController)
    let searchBar = searchController.searchBar
    searchBar.placeholder = searchBarPlaceHolderText
    searchBar.sizeToFit()
    tableView.tableHeaderView = searchBar
    searchController.searchResultsUpdater = resultsController

and then implement the protocol

class MyNavTableViewController: UIViewController, UITableViewDataSource, SelectedCellProtocol{
    func didSelectedCell(text: String) {
        print(text)
        //make your custom segue
    }
}


来源:https://stackoverflow.com/questions/35178691/swift-search-result-controller-in-search-results-segue-to-another-view-controlle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!