【发布时间】:2016-02-26 03:20:05
【问题描述】:
我似乎正在关注 tee 的教程,但我无法让我的数据在不为零的情况下传递给我的第二个视图控制器。 我是 Swift 新手,所以我可能也没有正确使用选项。这是我设置代码的方式。
import UIKit
class DetailsViewController: UIViewController {
var book : PLBook?
override func viewDidLoad() {
super.viewDidLoad()
//View Did Load
print(self.book)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
}
然后这就是我展示第二个视图控制器并为 segue 做准备的方式。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailVC", sender:self)
}
//MARK: Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toDetailVC"){
//prepare for segue to the details view controller
if let detailsVC = sender?.destinationViewController as? DetailsViewController {
let indexPath = self.tableView.indexPathForSelectedRow
detailsVC.book = self.books[indexPath!.row]
}
}
}
在第二个视图控制器中,book var 始终为 nil。有什么想法可以让数据通过吗?
【问题讨论】:
-
你有多确定
detailsVC.book不为零? -
print(self.book) 无日志
-
ye 但是在 segue 中分配它之后打印
detailsVC.book以确保你实际上没有传递它 nil,如果它是 nil,那么你的 self.books 数组有问题而不是传递你的数据在一个 segue 上
标签: ios uinavigationcontroller swift2 segue