Create and execute segue without storyboard

I have an application without a storyboard, all the user interface creation is done in code, and I got a splitView that I would make it suitable for use on the iPhone, because since the application was first developed only for the iPad, so when you select the line in the list in the "Master" view does nothing on the iPhone, but works fine on the iPad.

So my question is, can I create and execute a segue, which allows me to show the Detail View using the didSelectRowAtIndexPath method?

Here is what I have 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 I started and selected the line, the application worked, saying that it needed a execution handler, so I added the following:

 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 you select the xcode string, it says that there is no segment with that identifier "test".

I also tried calling it segue.perform() and adding the contents of the performHandler to the prepareForSegueMethod method:

  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 doesnโ€™t do anything, it doesnโ€™t crash, just select the selected line i

Can you guys help me?

EDIT: As Oleg Gordyuchuk said, it's impossible to do what I want to do without a storyboard, so thanks for the help :)

+7
ios iphone xcode swift
source share
2 answers

Segue is a component of interaction with the storyboard, you can understand by the name of the class UIStoryboardSegue . It is a bad idea to create segues programmatically. If I'm not mistaken, then the storyboard creates them for you.

To solve the problem, you are trying to use some general methods, for example, just submit a ViewController .

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

As I understand from our conversation in the comments. You would like to create a navigation to view in a table using segues without a storyboard. While this is impossible to do without a storyboard.

For future study, try researching this information .

+7
source share

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) } 
0
source share

All Articles