【问题标题】:segue from UIViewController to UITableViewController从 UIViewController 转到 UITableViewController
【发布时间】:2016-01-16 14:53:55
【问题描述】:

我正在尝试将 segue 数据从 UIViewController 转移到 UITableViewController。 UIViewController 中的数据来自 PFTableViewController,我想将该数据传递给另一个 TableViewController。

使用didSelect 方法传递给ViewController,但我不知道如何从viewController 切换到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)

}

我是编程新手。

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

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

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

    然后您可以在 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)
    }
    

    当然,您需要实现 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!
        }
    }
    

    编辑:

    如果您想执行从UIButton 到UITableViewController 的转场,逻辑相同,只需在情节提要中创建自定义转场,然后调用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
       }
    }
    

    希望对你有所帮助。

    【讨论】:

    • 谢谢@Victor,但是有一个问题我不能使用上面的语法,因为我正在尝试从UIViewController 到UITableViewController 并且上面的语法可以在UITableViewController 协议中使用到现在为止我学到了多少Swift and Xcode
    • @SabhaySardana 您想如何从UIButton 手动或其他方式进行转场?
    • 在 ViewController 中带有按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多