在prepareForSegue中传递数据(Swift 2)

时间:2016-04-09 13:28:24

标签: ios swift

当用户点击tableview中的行时,我正在尝试导航到viewcontroller(JobDetailsVC)。我对在Swift 2中做事情有点困惑,目前我收到错误'无法指定类型'JobDetailsTableVC.datastruct'的值来键入'JobDetails?'。

以下是我的准备工作中的代码

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

       let JobDetailsViewController = segue.destinationViewController as! JobDetailsVC

         if let selectedJobCell = sender as? JobDetailsCell {
            let indexPath = tableView.indexPathForCell(selectedJobCell)!
            let selectedJob = TableData[indexPath.row]

            print(selectedJob)
            JobDetailsViewController.Job = selectedJob
        }
    }

selectedJob正在正确分配我要传递的数据,例如;

datastruct(日期:可选(“2016年3月12日”),参考:可选(“这是参考”),开始:可选(“上午11:38”),结束:可选(“下午6:40” “),description:可选(”这是一个描述“),duration:Optional(”7:10“),job_id:Optional(”348“),token:Optional(”11dc886610“),notes:nil) []

数据结构来自

    struct datastruct
{
    var date:String?
    var reference:String?
    var start:String?
    var end:String?
    var description:String?
    var duration:String?
    var job_id:String?
    var token:String?
    var notes:String?



    init(add: NSDictionary)
    {
        date = add["date"] as? String
        reference = add["reference"] as? String
        start = add["start"] as? String
        end = add["end"] as? String
        description = add["description"] as? String
        job_id = add["job_id"] as? String
        token = add["token"] as? String
        duration = add["duration"] as? String
        notes = add["notes"] as? String
    }
}

在当前视图控制器中,我有:

var Job = [JobDetails]()

在视图控制器中我想将数据发送到(JobDetailsVC)我有这个:

var Job: JobDetails?

我在类

中声明了JobDetails
class JobDetails {

var date:String?
var reference:String?
var start:String?
var end:String?
var description:String?
var duration:String?
var job_id:String?
var token:String?
var notes:String?

init(date: String, reference: String, start: String, end: String, description: String, duration: String, job_id: String, token: String, notes: String)
{

    self.date = date
    self.reference = reference
    self.start=start
    self.end=end
    self.description=description
    self.job_id=job_id
    self.token=token
    self.notes=notes
    self.duration=duration

}

}

任何人都可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:-1)

请注意,您将Job定义为JobDetails数组:

var Job = [JobDetails]()

一旦选择作业只是一个项目,你就不能这样做:

JobDetailsViewController.Job = selectedJob

只需将Job的定义更改为:

var Job = JobDetails()

但是如果你想在你的VC上保留Job作为一个工作数组,那就这样做:

JobDetailsViewController.Job += [selectedJob]