I have this:
MyTableViewController
(inherits fromUITableViewController
)- It has a dynamic tableview with a few cells (foo, bar, qux)
- It has a dynamic tableview with a few cells (foo, bar, qux)
MyViewController
(inherits fromUIViewController
)- 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:
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 theviewController
in a new property in yourtableViewController
, let's name itparentController
. And then you'll have just to callself.parentController.performSegueWithIdentifier()
.EDIT: But first of all, maybe you can use the existing
parentViewController
if it contains the embedding view controller.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.
No need to create a property. Just this
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
Now you can simply invoke segues like
Hope this helps
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: