我正在寻找想法如何解决我从第一个视图控制器到第二个视图控制器的数据转换问题。当用户选择表格单元格时,将调用第二个视图控制器。
填充第一个tableview的代码
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = mtgRates.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
cell.fiName.text = fetchedFiName[indexPath.row].fiName
cell.oneYear.text = fetchedFiName[indexPath.row].oneYear
cell.twoYear.text = fetchedFiName[indexPath.row].twoYear
cell.threeYear.text = fetchedFiName[indexPath.row].threeYear
cell.fourYear.text = fetchedFiName[indexPath.row].fourYear
cell.fiveYear.text = fetchedFiName[indexPath.row].fiveYear
return (cell)
}
我观看了许多YouTube视频,但在使用全局数组设置数据时,它们都采用了一种简单的方法。
我一直在工作的代码,但此时什么也没做。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
oneYearFound = self.fetchedFiName[indexPath.row].oneYear
twoYearFound = self.fetchedFiName[indexPath.row].twoYear
threeYearFound = self.fetchedFiName[indexPath.row].threeYear
fourYearFound = self.fetchedFiName[indexPath.row].fourYear
fiveYearFound = self.fetchedFiName[indexPath.row].fiveYear
performSegue(withIdentifier: "segue", sender: self)
}
我在想我的问题是将获取的结果发送到第二个视图控制器
感谢您的帮助!
基于回复的更多信息。你是对的我在故事板上有两个视图控制器。我到目前为止我的UIViewController的代码是
class SegueViewController: UIViewController {
@IBOutlet weak var V2TwoYear: UILabel!
@IBOutlet weak var V2FiveYear: UILabel!
@IBOutlet weak var V2FourYear: UILabel!
@IBOutlet weak var V2ThreeYear: UILabel!
@IBOutlet weak var V2OneYear: UILabel!
@IBOutlet weak var V2FiName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
V2FiName.text = foundFi[myIndex].fiName
V2TwoYear.text = foundFi[myIndex].twoYear
V2OneYear.text = foundFi[myIndex].oneYear
V2ThreeYear.text = foundFi[myIndex].threeYear
V2FourYear.text = foundFi[myIndex].fourYear
V2FiName.text = foundFi[myIndex].fiveYear
}
答案 0 :(得分:1)
为什么不传递fetchedFiName的实例?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row]
performSegue(withIdentifier: "segue", sender: selectedFiName)
}
然后,将您的发件人转发为YOUR_FETCHED_FI_NAME_CLASS
并将其传递给prepareForSegue中的目标视图控制器:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedFiName = sender as? YOUR_FETCHED_FI_NAME_CLASS,
destVC = segue.destination as? SegueViewController {
destVC.passedFiName = selectedFiName
}
}
然后,在目标viewController中和ViewDidLoad之后(因为之前不会加载标签),您可以使用passedFiName
填充标签。
override func viewDidLoad() {
super.viewDidLoad()
updateLabels()
}
func updateLabels() {
V2FiName.text = passedFiName.fiName
V2TwoYear.text = passedFiName.twoYear
V2OneYear.text = passedFiName.oneYear
V2ThreeYear.text = passedFiName.threeYear
V2FourYear.text = passedFiName.fourYear
V2FiName.text = passedFiName.fiveYear
}
更新
我仍然有这个问题。我想我越来越近了
控制器一个代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row].fiName
let selectedOneYear = self.fetchedFiName[indexPath.row].oneYear
let selectedTwoYear = self.fetchedFiName[indexPath.row].twoYear
let selectedThreeYear = self.fetchedFiName[indexPath.row].threeYear
let selectedFourYear = self.fetchedFiName[indexPath.row].fourYear
let selectedFiveYear = self.fetchedFiName[indexPath.row].fiveYear
passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]
performSegue(withIdentifier: "SecondViewController", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedFiName = sender as! ,
let destVC = segue.destination as? SecondViewController {
destVC.fiName = selectedFiName
}
}
第二视图控制器代码`
struct SecondTable {
var passedFIName: String = ""
var passedOneYear: String = ""
var passedTwoYear: String = ""
var passedThreeYear: String = ""
var passedFourYear: String = ""
var passedFiveYear: String = ""
}
类SecondViewController:UIViewController {
@IBOutlet weak var fiName: UILabel!
@IBOutlet weak var sometext: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let fiDetails = SecondTable()
fiName.text = .passedFIName
sometext.text = "Some Text"
}
}
我在`override func prepare收到错误消息(对于segue:UIStoryboardSegue,sender:Any?){ 我不知道该怎样放待" Sender as" Missing value"
我一直在寻找几个小时。一个我解决了这个问题。我的项目将开始前进。谢谢你的帮助!“
答案 1 :(得分:0)
我需要做一些假设:
听起来像SecondViewController需要某种对象来保存这些值。在该视图控制器上定义一个,然后在FirstViewController上创建一个prepareForSeque方法。 Source
在prepareForSeque中,从目标视图控制器中取出seque,然后将对象传递给它。
然后使用该对象填充ViewDidAppear或ViewDidLoad中标签的值。
编辑:
看起来你真的很亲密。确保您不要忘记在第一个视图控制器中包含PrepareForSeque方法。在该方法覆盖内部,您可以访问第二个视图控制器实例并设置这些数组值。
以下是一些有助于您思考的代码。
class LabelsClass {
var str1:String;
var str2:String;
init(firstName:String, secondName:String) {
str1 = firstName;
str2 = secondName;
}
}
class SegueViewController: UIViewController {
var firstLabelString:String = ""
var secondLabelString:String = ""
func setValues(labels:LabelsClass) {
self.firstLabelString = labels.str1;
self.secondLabelString = labels.str2;
}
}
class MessageListViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var first = "Game"
var second = "maniac"
var localLabelsVar = LabelsClass.init(firstName: first,secondName: second);
var destVC = segue.destination as? SegueViewController
destVC?.setValues(labels: localLobalsVar)
}
}