sports
sports

Reputation: 8147

Perform a parent segue from the embedded view controller

I have this:

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»?

Upvotes: 13

Views: 9162

Answers (6)

malhal
malhal

Reputation: 30773

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.

Upvotes: 0

Nox
Nox

Reputation: 1583

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)

Upvotes: 0

Chris8447
Chris8447

Reputation: 306

No need to create a property. Just this

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

Upvotes: 3

Zaphod
Zaphod

Reputation: 7290

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.

Upvotes: 9

gjeck
gjeck

Reputation: 313

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

Upvotes: 3

Sarim Sidd
Sarim Sidd

Reputation: 2176

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

Upvotes: -1

Related Questions