senty
senty

Reputation: 12847

Performing Segue from - TableViewCell.xib to UIView - and - TableView.xib to UIViewController -

How can I perform segue from one .xib (TableCell.xib) to another .xib(UIViewController) on click on TableCell?

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(results?[indexPath.row])  // prints successfully
      ... 
      // apply below chunks here...
  }

First, I tried setting up a manual segue in Storyboard. Note that, TableView & TableViewCell are external xib but parent of TableView is in Storyboard. So, I tried control-dragging from parent-ViewController to the detailViewController (and identifier: showDetails)..However, doesn't work.

      ...
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.performSegueWithIdentifier("showDetails", sender: AnyObject?())
        })
      ...

Obviously, I received this error (because TableView is in .xib whereas parent and detailsVC are in Storyboard and I ctrl+dragged from parentVC to detailsVC but TableView1 apparently doesn't recognise the segue in Main Storyboard):

TableView1: 0x7f97626a3280>) has no segue with identifier 'showDetails''


Then I tried creating a ViewController and tried adding var parentNavigationController : UINavigationController! at the top and referencing below chunk inside didSelectRowAtIndex

   ...
     let newVC : UIViewController = CellDetailsVC()
     newVC.title = "DetailsView"
     print(newVC)

     parentNavigationController!.pushViewController(newVC, animated: true)

However, I am receiving an error:

DetailsVC: 0x7ff7794ae550> fatal error: unexpectedly found nil while unwrapping an Optional value


Finally, I also tried below chunk inside didSelectRowAtIndex, and got something close (as I can use dismissView to < Back); but not exactly what I want. Its problem is that the segue animation looks like the page is coming from bottom.

   ...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("VideoDetailsVC") as UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
 }

I am also receiving a warning (but not crash) for some reason I couldn't figure out:

Presenting view controllers on detached view controllers is discouraged


Upvotes: 1

Views: 1172

Answers (1)

sanjana
sanjana

Reputation: 3034

Your second approach looks to be right one but of course you are doing something wrong there. You need to drag and drop Segue from Parent ViewController to Detail ViewController and give it a Segue identifier in Storyboard (check attached Image below). IN didSelectRowAtIndexPath you need to do below code:

    self.performSegueWithIdentifier("showDetailViewController", sender: nil)

As I can see you already tried that but it looks like you missed some step either setting Segue Identifier or giving wrong identifier in performSegueWithIdentifier method. Also your Parent ViewController should be embedded in UINavigationController in case you are missing that thing :) enter image description here

Also keep in mind you should perform navigation (here its Segue) from one ViewController to another ViewController at same level not from one View to another ViewController.

Upvotes: 3

Related Questions