13

I have this:

  • MyTableViewController (inherits from UITableViewController)

    • It has a dynamic tableview with a few cells (foo, bar, qux)

  • MyViewController (inherits from UIViewController)

    • There are some "show" segues from this controller to other view controllers
    • It has a UIContainerView that embeds MyTableViewController

A picture speaks a thousand words:

embedded uitableviewcontroller

When a certain cell is selected, I want to perform a segue of the parent view (MyViewController)

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         if (indexPath.section == 1 && indexPath.row == 1) {
                self.WHAT.performSegueWithIdentifier("someShowSegue1", sender: self)
         }
  }

Is it possible? what should I use in «WHAT»?

0

6 Answers 6

9

In the prepareForSegue: for your embedded segue set the viewController in a new property in your tableViewController, let's name it parentController. And then you'll have just to call self.parentController.performSegueWithIdentifier().

EDIT: But first of all, maybe you can use the existing parentViewController if it contains the embedding view controller.

7
  • 4
    Every child view controller can directly access their parent using parentViewController. We dont have to name it, it's already available. Look through UIViewController header :)
    – Sandeep
    Jun 3, 2015 at 18:53
  • Oh yeah, that's true, but it's readonly... I'll rename it in my answer.
    – Zaphod
    Jun 3, 2015 at 19:00
  • Indeed using parentViewController was enough. Although the idea was good :)
    – sports
    Jun 4, 2015 at 0:12
  • this is a great answer, but what if there is data in the child view controller that needs to be passed along in the segue? how do you pass it? because the prepareForSegue function that gets called will be the one in the parent view controller
    – A. Vin
    May 3, 2016 at 4:20
  • Does it still work if the parent and child view controllers have different navigation controllers? Oct 20, 2016 at 6:38
3

You may want to consider using delegation to solve this problem since the child tableView doesn't seem like it should be responsible for the segue. For example:

// MyViewController
class MyViewController: UIViewController, MyTableViewControllerDelegate {

    func selectedMyTableViewControllerCell(cell: UITableViewCell) {
        // ... check cell type or index or whatever
        self.performSegueWithIdentifier("someValueFromCellType", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == myTableViewControllerIdentifier {
            if let vc = segue.destinationViewController as MyTableViewController? {
                vc.delegate = self
            }
        }
    }
}

// MyTableViewController
protocol MyTableViewControllerDelegate: class {
    func selectedMyTableViewControllerCell(cell: UITableViewCell)
}

class MyTableViewController: UITableViewController {
    weak var delegate: MyTableViewControllerDelegate?

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // ... get the cell
        delegate?.selectedMyTableViewControllerCell(cell)
    }
}
3

No need to create a property. Just this

self.parent?.performSegue(withIdentifier: "ID", sender: self)
0

SWIFT 4

Swift 4 no longer has parentViewController. You must use parent to access the parent object and is an optional so be sure to check for nil where necessary.

self.parent?.performSegue(withIdentifier: "IdentifierHere", sender: self)
1
  • Swift 4 no longer has parentViewController No, Swift itself is just an agnostic language and doesn't contain anything UI related. This change is not due to Swift 4. It's because the method changed in the framework - in UIKit.
    – Eric Aya
    Feb 13, 2019 at 16:41
0

Hook your segue up to the embedded table view controller's cell instead. You can use different segues per cell prototype. This saves you from checking index paths or even implementing didSelectRow at all.

-1

Segue is defined from one view controller to another and is only invoke from the view controller in which it is defined. So you would need to store the reference of the parentViewController.

Like from MyViewController

if ([segueName isEqualToString: @"embedseg"]) {
    MyTableViewController * tblViewController = (MyTableViewController *) [segue destinationViewController];
    tblViewController.parentController=self;  //Storing reference of parentViewController i.e  MyViewController 
}

Now you can simply invoke segues like

self.parentController.performSegueWithIdentifier("someShowSegue1", sender: self)

Hope this helps

1
  • You should't store the parentController in the child. The embedded table view controller can access its parent by calling parentViewController.
    – malhal
    Aug 30, 2019 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.