Rahul Chaurasia Rahul Chaurasia - 5 months ago 19
Swift Question

How to performSegueWithIdentifier() from dynamically created UIViewController whose storyboard property is nil

I'm working on a project with has a search functionality.I've 3 view controllers i.e. HomeViewController, SearchResultViewController and DetailViewController.

On the main page, I've a search bar (using UISearchController) within the navigationBar along with some dummy content. Important thing here is that I'm using separate controller (in my case it is SearchResultController) to show the results. This SearchResultController is actually subclass of TableViewController which displays the result in tableview. Everything is working fine till here.


Now, when I’m selecting any row from the result ( i.e. at SearchResultViewController) I want to open another view controller (DetailViewController) where I’ll be showing the details about this selected item. It is a very simple thing but I'm stuck here .

Okay, continuing with the problem I’ve implemented didSelectRowAtIndexPath method and from that I’m calling performSegueWithIdentifier and I’m getting error “Receiver has no segue with identifier” because the storyboard property is nil (It is mentioned in the documentation).

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("storyboard property is \(self.storyboard)")
performSegueWithIdentifier(segueIdentifier, sender: indexPath.row)

segueIdentifier is same as it on the storyboard.
I'm getting that storyboard property is nil.

I guess

When I tap on the search bar, iOS internally creates and loads the instance of the SearchResultController to display the result and therefore its storyboard property is nil and because of that I'm getting the above error.

Can somebody give solution to this problem or any pointer to the right direction.


If you are creating viewcontroller programmatically then you should push in on navigation controller like,

  UIViewController *vc = [[UIViewController alloc]init]; //your viewcontroller here

[self.navigationController pushViewController:vc animated:YES];

//or if you want to present it then

[self presentViewController:vc animated:YES completion:nil];

You can't perform segue because there is no segue without storyboard

Something like this in swift

 let vc: UIViewController = UIViewController()
    //your viewcontroller here
    self.navigationController!.pushViewController(vc, animated: true)
    //or if you want to present it then
    self.presentViewController(vc, animated: true, completion: { _ in })

Avoid any mistake in swift!!!

hope this will help :)