我是swift的新手,学习swift 3
我试图将数据从表视图控制器传递到XIB文件。我的表视图控制器中有水果列表。点击它我想在新的XIB控制器的标签中显示水果名称。我尝试下面的代码,但它没有向我显示XIB中的任何数据vc ..请告诉我我在这里缺少什么
我的TableVC:
reg = MLPRegressor(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
第二个VC:
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)
}
}
答案 0 :(得分:0)
问题在于代码中的以下行:
self.present(ShowDetailsXibViewController(), animated: true, completion: nil)
您在那里实例化ShowDetailsXibViewController的新实例,并且不使用您已使用此行创建的实例:
let detailsVC = ShowDetailsXibViewController(nibName: "ShowDetailsXibViewController", bundle: nil)
如果您将第一行更改为以下内容,则应该有效:
self.present(detailsVC, animated: true, completion: nil)
答案 1 :(得分:0)
问题在于这一行:
self.present(ShowDetailsXibViewController(), animated: true, completion: nil)
在这里,您要创建另一个ShowDetailsXibViewController
。而不是呈现先前创建的控制器,您应该写:
self.present(detailsVC, animated: true, completion: nil)