我似乎正在关注T恤的教程,但是如果没有nil,我就无法将数据传递给第二个视图控制器。
我是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]
}
}
}
在第二个视图控制器中,书籍var始终为nil。有什么想法可以使数据通过吗?
最佳答案
您应该使用segue.destinationViewController
,而不是sender?.destinationViewController
。
关于ios - 使用Swift 2.0在Segue之间传递数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35642811/