Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and perform segue without storyboards

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 :)

like image 952
EddyW Avatar asked Mar 25 '16 08:03

EddyW


2 Answers

Segue it is component of the storyboard interaction it is possible to understand from name of the class UIStoryboardSegue. It is bad idea to create segues programmatically. If i am not making mistake storyboard creates them for you.

For solving of you're issue try to use some common ways like simply present ViewController.

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("id") as! MyController
self.presentViewController(vc, animated: true, completion: nil)

As i understand from our conversation in comments. You would like to create navigation for tableview to details view using segues without storyboard. For now it is impossible to do this without storyboard.

For future learning try to investigate this information.

like image 170
Oleg Gordiichuk Avatar answered Oct 06 '22 18:10

Oleg Gordiichuk


One way is to use the didSelectRow method for tableView

Swift 3.x

// MARK: - Navigation & Pass Data
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("Selected Row \(indexPath.row)")
    let nextVC = YourNextViewController()
    nextVC.YourLabel.text = "Passed Text"
    nextVC.YourLabel.text = YourArray[indexPath.row]

    // Push to next view
    navigationController?.pushViewController(nextVC, animated: true)
}
like image 4
Mohamad Kaakati Avatar answered Oct 06 '22 18:10

Mohamad Kaakati