将数据从 TableViewController 传递到 XIB 文件而无需 Segues-Swift 3 [英] Pass Data from TableViewController to XIB file Without Segues- Swift 3

查看:31
本文介绍了将数据从 TableViewController 传递到 XIB 文件而无需 Segues-Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,正在学习 swift 3

Im new to swift and learning swift 3

我正在尝试将数据从表视图控制器传递到 XIB 文件.我的表视图控制器中有水果列表.单击该按钮后,我想在新 XIB 控制器的标签中显示水果名称.我尝试了下面的代码,但它没有向我显示 XIB vc 中的任何数据..请告诉我我在这里遗漏了什么

Im trying to pass data from table view controller to XIB file . I have list of fruits in my table view controller. On click of that i would like to display fruit name in a label in new XIB controller. I tried below code but it is not showing me any data in XIB vc..please tell me what am I missing here

我的 TableVC:

My TableVC:

class FruitsTableViewController: UITableViewController {

    var fruits = ["Apple", "Apricot", "Banana", "Blueberry", "Cantaloupe", "Cherry",
                  "Clementine", "Coconut", "Cranberry", "Fig", "Grape", "Grapefruit",
                  "Kiwi fruit", "Lemon", "Lime", "Lychee", "Mandarine", "Mango",
                  "Melon", "Nectarine", "Olive", "Orange", "Papaya", "Peach",
                  "Pear", "Pineapple", "Raspberry", "Strawberry"]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = fruits[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dataToPass = fruits[indexPath.row]
        let detailsVC = ShowDetailsXibViewController(nibName: "ShowDetailsXibViewController", bundle: nil)
        detailsVC.dataFromVC = dataToPass
        self.present(ShowDetailsXibViewController(), animated: true, completion: nil)

    }

}

第二个 VC:

class ShowDetailsXibViewController: UIViewController {

    @IBOutlet weak var lblFruit: UILabel!

    var dataFromVC : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        lblFruit.text = dataFromVC
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

推荐答案

问题在于您的代码中的以下行:

The issue is with the following line in your code:

self.present(ShowDetailsXibViewController(), animated: true, completion: nil)

你在那里实例化一个 ShowDetailsXibViewController 的新实例,不要使用你已经用这一行创建的实例:

You instantiate a new instance of ShowDetailsXibViewController there and do not use the one you already created with this line:

let detailsVC = ShowDetailsXibViewController(nibName: "ShowDetailsXibViewController", bundle: nil)

如果您将第一行更改为以下内容,它应该可以工作:

If you change the first line to the following, it should work:

self.present(detailsVC, animated: true, completion: nil)

这篇关于将数据从 TableViewController 传递到 XIB 文件而无需 Segues-Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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