简体   繁体   English

从UIViewController选择到UITableViewController

[英]segue from UIViewController to UITableViewController

I'm trying to segue data from UIViewController to UITableViewController . 我想segue数据从UIViewControllerUITableViewController The data in UIViewController is coming from PFTableViewController and I want to pass that data to another TableViewController . UIViewController的数据来自PFTableViewController ,我想将该数据传递给另一个TableViewController

For passing to ViewController didSelect method is used but I have no idea how can I segue from viewController to TableViewController . 为了传递给ViewController使用了didSelect方法,但我不知道如何从viewControllerTableViewController

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 . 你的情况,你可以从你的手动启动SEGUE didSelectRowAtIndexPath ,用该方法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: 您需要在Storyboard中从UIViewControllerUITableViewController创建自定义序列,并为其设置一个Identifier ,您可以按Ctrl +从UIViewController到下一个Click以创建自定义序列,然后选择该序列并在Attributes Inspector中只需为这张图片设置segue的标识符:

在此处输入图片说明

Then you can in your didSelectRowAtIndexPath launch the segue manually as in this code: 然后,您可以按照以下代码在didSelectRowAtIndexPath手动启动segue:

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: 当然,您需要实现方法prepareForSegue以将数据从UIViewController传递到UITableView如以下代码所示:

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: 如果要从UIButtonUITableViewController执行序列化,这是相同的逻辑,只需在Storyboard中创建自定义序列化,然后像下面的代码中那样调用prepareForSegue

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. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从UIViewController到UITableViewController的Segue无效 - Segue from UIViewController to UITableViewController has no effect 如何以编程方式从 UITableViewController 顺利切换到 UIViewController - How to segue smoothly from UITableViewController to UIViewController programmatically 如何在从UITableViewController到UIViewController的展开中声明UITableViewController - How to declar UITableViewController in Unwind segue from UITableViewController to UIViewController 使用数组值Swift3从UITableViewController到UIViewController执行Segue - Perform Segue from UITableViewController to UIViewController with Array Value Swift3 从Xib中选择到UIViewController - Segue to UIViewController from a xib 应用因UITableViewController的选择而崩溃 - App crashes on segue from UITableViewController 将标记从UITableViewController添加到UIViewController - Add markers from UITableViewController to UIViewController 从UITableViewController将NSFetchedResultsController迁移到UIViewController - Migrating NSFetchedResultsController to UIViewController from UITableViewController 从UITableView(不是UITableViewController)转到UIViewController - Go from UITableView(not a UITableViewController) to a UIViewController 未通过segue连接在UIViewController和UITableViewController之间传递数据 - Passing data between UIViewController and UITableViewController not connected via segue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM