SWIFT - 你如何传递所有数据?
SWIFT - How do you pass through all data?
我有一个数组对象,我想通过 segue 并在连接的页面上使用它。
第一页采用此数组并将其传递给 segue,如下所示:
//home.swift
//Data to pass through
let personInfo:[Person] = [Person(name: "Jess", admin: "jess17", image: "jess17.png", age:"20"), Person(name: "Matt", admin: "matty101", image: "matty101.png", age:"23"), Person(name: "Tom", admin: "thomas9", image: "thomas9.png", age:"21")]
//Segue information
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "openAccount" {
let indexPath = voteCollectionView!.indexPathForCell(sender as CollectionViewCell)
let destinationController = segue.destinationViewController as AccountPageViewController
destinationController.peopleData = personInfo
}
}
在 AccountPageViewController 上,personData 数组被以下对象捕获:
var peopleData:[Person]!
但是,当将它附加到 UILabel 时,我得到一个 nil object 错误。我尝试使用以下方式附加它:
headerLbl.text = peopleData[0].name
如有任何帮助,我们将不胜感激!
你在哪里打电话 headerLbl.text = peopleData[0].name
。您确定收到的 nil 错误是由数组为 nil 而不是实际标签为 nil 触发的吗?如果您试图在您的视图实际呈现之前访问标签,它将为零。
我有一个数组对象,我想通过 segue 并在连接的页面上使用它。
第一页采用此数组并将其传递给 segue,如下所示:
//home.swift
//Data to pass through
let personInfo:[Person] = [Person(name: "Jess", admin: "jess17", image: "jess17.png", age:"20"), Person(name: "Matt", admin: "matty101", image: "matty101.png", age:"23"), Person(name: "Tom", admin: "thomas9", image: "thomas9.png", age:"21")]
//Segue information
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "openAccount" {
let indexPath = voteCollectionView!.indexPathForCell(sender as CollectionViewCell)
let destinationController = segue.destinationViewController as AccountPageViewController
destinationController.peopleData = personInfo
}
}
在 AccountPageViewController 上,personData 数组被以下对象捕获:
var peopleData:[Person]!
但是,当将它附加到 UILabel 时,我得到一个 nil object 错误。我尝试使用以下方式附加它:
headerLbl.text = peopleData[0].name
如有任何帮助,我们将不胜感激!
你在哪里打电话 headerLbl.text = peopleData[0].name
。您确定收到的 nil 错误是由数组为 nil 而不是实际标签为 nil 触发的吗?如果您试图在您的视图实际呈现之前访问标签,它将为零。