Reputation: 886
I'm trying to segue
data from UIViewController
to UITableViewController
.
The data in UIViewController
is coming from PFTableViewController
and I want to pass that data to another TableViewController
.
For passing to ViewController didSelect
method is used but I have no idea how can I segue from viewController
to TableViewController
.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let destinationToFVC:ViewController = self.storyboard!.instantiateViewControllerWithIdentifier("FVCont") as! ViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
let row1 = Int(indexPath.row)
destinationToFVC.currentObject = objects?[row1] as! PFObject!
}
navigationController?.pushViewController(destinationToFVC, animated: true)
}
I'm new to programming.
Upvotes: 0
Views: 1687
Reputation: 23451
In your case you can launch the segue manually from your didSelectRowAtIndexPath
, with the the method performSegueWithIdentifier
.
You need to create a custom segue in your Storyboard from your UIViewController
to your UITableViewController
and set an Identifier
for it, you can press Ctrl
+ Click
from your UIViewController
to the next to create the custom segue and then select the segue and in the Attributes Inspector set the Identifier for the segue just a this picture:
Then you can in your didSelectRowAtIndexPath
launch the segue manually as in this code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// the identifier of the segue is the same you set in the Attributes Inspector
self.performSegueWithIdentifier("mySegueID", sender: self)
}
And of course you need to implement the method prepareForSegue
to pass the data from your UIViewController
to your UITableView
like in this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "mySegueID") {
// in this line you need to set the name of the class of your `UITableViewController`
let viewController = segue.destinationViewController as UITableViewController
let indexPath = self.tableView.indexPathForSelectedRow()
destinationToFVC.currentObject = objects?[indexPath] as! PFObject!
}
}
EDIT:
If you want to perform your segue from an UIButton
to the UITableViewController
it's the same logic, just create the custom segue in your Storyboard and then call the prepareForSegue
like in the following code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "mySegueID") {
// in this line you need to set the name of the class of your `UITableViewController`
let viewController = segue.destinationViewController as UITableViewController
/ here you need to pass the data using the reference viewController
}
}
I hope this help you.
Upvotes: 1