简体   繁体   中英

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.

在此处输入图片说明

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

}

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

Use this code,

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
       self.myFavText = "My Favorite Text"
       self.performSegueWithIdentifier("favDetails", sender: self)

}

you will pass the value in Container ViewController class, then assign the value to TableViewControllerEmbeddedInContainer class.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if (segue.identifier == "favDetails") {

       var myDetails = segue.destinationViewController as! ContainerViewController
       myDetails.myFav = self.myFavText 

    }
}

hope its helpful.

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