简体   繁体   中英

Perform a parent segue from the embedded view controller

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:

嵌入式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» ?

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.

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

No need to create a property. Just this

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

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.

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM