从 UIViewController 到 UITableViewController [英] segue from UIViewController to UITableViewController

查看:21
本文介绍了从 UIViewController 到 UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 UIViewController 中的数据 segue 转换为 UITableViewController.UIViewController 中的数据来自 PFTableViewController,我想将该数据传递给另一个 TableViewController.

为了传递给 ViewController 使用了 didSelect 方法,但我不知道如何从 viewController 转到 TableViewController.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {让 destinationToFVC:ViewController = self.storyboard!.instantiateViewControllerWithIdentifier("FVCont") as!视图控制器如果让 indexPath = self.tableView.indexPathForSelectedRow {让 row1 = Int(indexPath.row)destinationToFVC.currentObject = objects?[row1] as!PF对象!}导航控制器? .pushViewController(destinationToFVC,动画:真)}

我是编程新手.

解决方案

在您的情况下,您可以使用 performSegueWithIdentifier 方法从 didSelectRowAtIndexPath 手动启动 segue.

您需要在您的故事板中创建一个从 UIViewController 到您的 UITableViewController 的自定义 segue 并为其设置一个 Identifier,您可以按Ctrl + Click 从您的 UIViewController 到下一个以创建自定义 segue,然后在 Attributes Inspector 中选择 segue 和strong> 为 segue 设置标识符只是这张图片:

然后您可以在您的 didSelectRowAtIndexPath 中手动启动 segue,如下代码所示:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {//segue 的标识符与您在 Attributes Inspector 中设置的相同self.performSegueWithIdentifier("mySegueID", 发件人:self)}

当然,您需要实现方法 prepareForSegue 以将数据从您的 UIViewController 传递到您的 UITableView,如下面的代码所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {如果(segue.identifier ==mySegueID"){//在这一行中,您需要设置您的`UITableViewController` 的类的名称让 viewController = segue.destinationViewController 作为 UITableViewController让 indexPath = self.tableView.indexPathForSelectedRow()destinationToFVC.currentObject = objects?[indexPath] as!PF对象!}}

<块引用>

如果您想执行从 UIButtonUITableViewController 的 segue,这是相同的逻辑,只需在 Storyboard 中创建自定义 segue,然后调用 prepareForSegue 就像下面的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {如果(segue.identifier ==mySegueID"){//在这一行中,您需要设置您的`UITableViewController` 的类的名称让 viewController = segue.destinationViewController 作为 UITableViewController/这里需要使用参考 viewController 传递数据}}

希望对您有所帮助.

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.

这篇关于从 UIViewController 到 UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆