Swift Search Result Controller in search results segue to another view controller
<p><strong>Problem:</strong> </p>

<p>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 <code>UISearchController</code> that handles the search bar and search result updating. The issue that I'm running into is that since my <code>SearchResultsController</code> 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.</p>

<p><strong>What I would like to do</strong></p>

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

<p><strong>If I can't get this to work what I will probably do:</strong></p>

<p>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. </p>

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

<p><a href="https://stackoverflow.com/questions/9674685/creating-a-segue-programmatically">Creating a segue programmatically</a></p>

<p><a href="https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html" rel="nofollow noreferrer">https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html</a></p>

<p><strong>My Setup</strong></p>

<p>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.</p>

<pre><code>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)


    }
}
</code></pre>

<p><strong>My Failed Attempts</strong></p>

<p>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.</p>

<p>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:</p>

<pre><code>let destVC = self.storyboard!.instantiateViewControllerWithIdentifier(imagePrepareStoryBoardId)

fatal error: unexpectedly found nil while unwrapping an Optional value
</code></pre>

<p>I double checked the imagePrepareStoryBoardId. It's correct.</p>

<p>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.</p>