Perform a parent segue from the embedded view cont

2020-05-23 08:40发布

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

6条回答
家丑人穷心不美
2楼-- · 2020-05-23 09:25

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.

查看更多
神经病院院长
3楼-- · 2020-05-23 09:30

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.

查看更多
【Aperson】
4楼-- · 2020-05-23 09:31

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)
查看更多
【Aperson】
5楼-- · 2020-05-23 09:34

No need to create a property. Just this

self.parent?.performSegue(withIdentifier: "ID", sender: self)
查看更多
Evening l夕情丶
6楼-- · 2020-05-23 09:37

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

查看更多
祖国的老花朵
7楼-- · 2020-05-23 09:41

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)
    }
}
查看更多
登录 后发表回答