1

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


  • Which approach is the right one? Or is there a better way to adapt to achieve what I am trying to?

  • Firstly, what is the right way to perform segue from didSelectRowAtIndex (in TableView.xib class) to a new ViewController (either Storyboard or xib)?

  • Secondly, assume that there is an image thumbnail in the TableViewCell.xib. What is right way of performing segue to a new UIView on click on that view? (like full-screen image / dismiss-able)

senty
  • 12,385
  • 28
  • 130
  • 260
  • how do you get parentNavigationController ? – Alok Rao Dec 07 '15 at 08:33
  • Add `var parentNavigationController : UINavigationController!` at the beginning of the TableView.xib class – senty Dec 07 '15 at 08:35
  • Think it as a social platform. Think TableView as a news feed. 1) On click on username -> perform segue to new VC showing profile. 2) On click on image-thumbnail -> full screen UIView. 3) On click on cell (anywhere else) - perform another segue to another UIViewController. So there is no way to achieve it in my case? – senty Dec 07 '15 at 08:50
  • sorry.. I am new to Swift. But as I see here you are declaring NavigationController.. where you are allocating or getting that NavigationController instance ? Yes, you can achieve that flow. – Alok Rao Dec 07 '15 at 08:55

1 Answers1

3

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.

sanjana
  • 3,034
  • 2
  • 25
  • 31
  • `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receive TableView1: >) has no segue with identifier 'showDetailVC'`` because parentVC is in storyboard but TableView is in xib file. So in Storyboard dragging from parentVC to detailVC and adding segue is okay, but TableView1 does not recognise it apparently. So unfortunately, your answer doesn't solve the problem. Do you have any other idea? – senty Dec 07 '15 at 09:13
  • Your app structure is not proper then. Your tableview doesn't need to have Segue. Would you mind sharing how exactly you are adding tableview to your ParentViewController? To add tableview in your ParentViewController you just need to drag and drop UITableView in controller. You should perform navigation (here its Segue) from one ViewController to another ViewController not from one View to another ViewController. – sanjana Dec 07 '15 at 09:29
  • Would you like to use zoom.us or skype for screen sharing? (You can control my mouse/keyboard with zoom.us). The project is quiet simple. So we can figure out the problem easily. Because my app is using rest-api (+ localhost) and you want be able to run it without tweak – senty Dec 07 '15 at 09:32