kampbell411 kampbell411 - 5 months ago 10
iOS Question

Passing Data from TableViewController to another TableViewController embedded in container

I'm having trouble passing data from a

TableViewController
to another
TableViewController
that is embedded in a
Container View
. When I pass the data in
prepareForSegue
I get hit with a "Could not cast value of type
UIViewController
to
TableViewControllerEmbeddedInContainer
. Do I have to create a file for the container
ViewController
and catch anything in there? Any help is greatly appreciated.

enter image description here

In my first TableViewController:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

self.myFavText = "My Favorite Text"
self.performSegueWithIdentifier("favDetails", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let myDetails = segue.destinationViewController as! TableViewControllerEmbeddedInContainer
details.myFav = self.myFavText

}

Answer

kampbell411,

"Do I have to create a file for the container ViewController and catch anything in there?"

Answer : YES.

"When I pass the data in prepareForSegue I get hit with a "Could not cast value of type 'UIViewController to TableViewControllerEmbeddedInContainer'"

Answer : You cant because segue is pointing to the ViewController containing UIContainerView and not the ViewController embeded in it :)

How to Solve :)

Create a class for ViewController containing ContainerView. Create an IBOutlet for ContainerView :)

Lets call it as Intermediate class and view as myContainer :)

class Intermediate: UIViewController {

    @IBOutlet var myContainer: UIView!
    var value : String!


    //catch the embeded segue get the ViewController reference lets call it as Testing 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let test = segue.destinationViewController as! Testing
        test.value = self.value
    }
}

This should help :)