简体   繁体   中英

segue from UIViewController to UITableViewController

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.

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.

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