Create and perform segue without storyboards

Viewed 13001

i got an app without storyboards, all UI creation is made in code and I got a splitView which I would make it usable on iPhone, because as the app as been first designed for iPad only, so that when you select a row in the list in the Master view it does nothing on iPhone but is working fine on iPad.

So my question is can I create and perform the segue that allows to show the Detail View on the didSelectRowAtIndexPath method ?

Here's what i've done so far :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!)
        performSegueWithIdentifier("test", sender: self)
    }

but when running and selecting a row the app was crashing telling it needed a performhandler so i added this :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!, performHandler: { () -> Void in
                let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
                let controller = self.detailViewController!
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true


        })
        performSegueWithIdentifier("test", sender: self)
    }

and now when selecting a row xcode says that there is no segue with such identifier "test".

I also tried to call it by segue.perform() and add the performHandler content into the prepareForSegueMethod :

 if segue.identifier == "test" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }

and it does just nothing, doesn't crash, just highlight the row i selected and that's all

Can you guys help me ?

EDIT : As Oleg Gordiichuk said, it's not possible to do what I want to do without Storyboards, so thanks for his help :)

2 Answers
Related