【问题标题】:Passing data between segue with Swift 2.0使用 Swift 2.0 在 segue 之间传递数据
【发布时间】: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


【解决方案1】:

试试这个:

override func prepareForSegue(segue: UIStoryboardSegue, sender:    AnyObject?)   {

if (segue.identifier == "toDetailVC") {

 //prepare for segue to the details view controller

           let detailsVC = segue.destinationViewController as!  DetailsViewController

            let indexPath = self.tableView.indexPathForSelectedRow
            detailsVC.book = self.books[indexPath!.row]

}

【讨论】:

    【解决方案2】:

    您应该使用segue.destinationViewController,而不是sender?.destinationViewController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多