Hos Ap
Hos Ap

Reputation: 1198

pushViewController does no action

i have a tableview and i want to go to another vc when one of rows tapped. my didSelectRowAtIndexPath function is as below. print command works and shows the right clicked row. but when i use self.navigationController?.pushViewController it does not go to vc with playVideo storyBoardId. after changing it to presentViewController it works. what's wrong about my code that push doesn't work?

thanks

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print("clicked " + String(indexPath.row) )
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("playVideo") as! PlayVideoViewController

    vc.id = self.video[indexPath.row].id

    // Present View as Modal
    //presentViewController(vc as UIViewController, animated: true, completion: nil)
    // Push View
    //self.navigationController?.pushViewController(vc, animated: true)
}

Upvotes: 1

Views: 4760

Answers (3)

Bharath
Bharath

Reputation: 2114

  • Its because the view controller you are currently in may not have a navigation controller.
  • You cannot push a view controller without having a UINavigationController.
  • If your requirement is to push a view controller, then perform the following steps.

    1. Embed a navigation controller for the current view controller.(Open storyboard -> Choose your view controller -> Choose "Editor" -> "Embed in" -> UINavigationController)
    2. Now your code

    self.navigationController?.pushViewController(vc, animated: true)

will be working fine.

Upvotes: 6

Boomerange
Boomerange

Reputation: 665

I dont know why are you doing it like this. I think its unnecessarily complicated.

This should by your function didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("playVideo", sender: tableView)
}

Then you can specify as much segues as you want in this function:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "playVideo") {
        let indexPath:NSIndexPath = tableView.indexPathForSelectedRow!
        let PlayVideoViewController = segue.destinationViewController as! PlayVideoViewController
        //you can pass parameters like project id
        detailVC.projectID = ids[indexPath.row]
    }
}

Your segue has to be named like this in interface builder:

named segue

Upvotes: 0

Bseaborn
Bseaborn

Reputation: 4407

Make sure that your ViewController is indeed built on a NavigationController. Try forcing the line:

self.navigationController!.pushViewController

if it crashes, you know there is not nav set up. Or you could just check in your storyboard but this is a quick way to tell.

Upvotes: 2

Related Questions