13

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

EddyW
  • 153
  • 1
  • 1
  • 7

2 Answers2

13

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.

Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • Thanks for your answer, I tried with pushViewController and it was replacing the view and was causing problems on iPhones 6 Plus and 6S Plus when in landscape mode. Your solution doesn't work for me, because I don't have any storyboards so self.storyboard always returns nil, but i got references of the two controllers in the splitView into to the AppDelegate file and still doesn't work "application tried to present modally an active controller" – EddyW Mar 25 '16 at 08:52
  • Use navigationcontroller to present view controllers on it. – Oleg Gordiichuk Mar 25 '16 at 08:53
  • tried with : let navC = UINavigationController(rootViewController: shared.mediaListController) self.presentViewController(navC, animated: true, completion: nil) And it works but does not what i'm looking for ^^' i just want the detail view showing just like in the xcode exemple of master/detail view on iPhones – EddyW Mar 25 '16 at 08:57
  • You would like to show detail view from tableview ? – Oleg Gordiichuk Mar 25 '16 at 09:01
  • Yes exactly because it's working fine on iPad as SplitView is handled differently between iPhones and iPad, exception made for the 6S Plus and the 6S Plus which are causing me problems ^^ just like the xcode exemple, a simple click on a row and the detail view appears, (i'm currently doing it with pushViewController and that seems to brake everything on iphones 6 plus) – EddyW Mar 25 '16 at 09:03
  • give me link to the resource you are using – Oleg Gordiichuk Mar 25 '16 at 09:07
  • you mean the exemple i'm speaking about ? – EddyW Mar 25 '16 at 09:23
  • i'm just speaking about the project template called "Master-Detail Application" when you create a new Project in Xcode , for them it works fine even on iphone 6plus – EddyW Mar 25 '16 at 09:27
  • They are using storyboards ) – Oleg Gordiichuk Mar 25 '16 at 09:32
  • can you please edit your answer saying that it's impossible to do what i want to do without storyboards so that i can accept your answer as solution – EddyW Mar 25 '16 at 10:41
4

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)
}
Mohamad Kaakati
  • 392
  • 3
  • 11