swift将数据从tableview单元格传递到视图控制器

时间:2015-11-02 16:06:21

标签: swift uitableview segue

我正在尝试将一些数据传递给单独的视图控制器,视图控制器连接到导航控制器,我可以使用

来访问此视图控制器

parentNavigationController!.performSegueWithIdentifier("ExercisePage", sender: self)

但数据没有传递,这是我的代码?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell2 = tableView.cellForRowAtIndexPath(indexPath) as! SelectionProperties


    let desVc = uploadDetails1()
desVc.workoutName = cell2.nameLabel!
         desVc.parentNavigationController!.performSegueWithIdentifier("ExercisePage", sender: self)
 }

它表示在打开可选项时发现了nil。

desVc.performSegueWithIdentifier("ExercisePage", sender: self) }

如果我这样做,就说没有赛道

1 个答案:

答案 0 :(得分:2)

试试这个:(你不必使用所有if语句......)

   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    performSegueWithIdentifier("ExercisePage", sender: indexPath)


    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
        {
            // Get the new view controller using segue.destinationViewController.

            if segue.identifier == "ExercisePage"
            {
                guard let indexPath = sender as? NSIndexPath else {
                return } 

                    guard let destinationViewController = segue.destinationViewController as? uploadDetails1 else { 
                        return } 
                        guard let cell = tableView.cellForRowAtIndexPath(indexPath) else { 
                         return } 

                       guard let _ = cell.nameLabel else { 
                          return } 

                            destinationViewController.workoutName = cell.nameLabel!
          }
        }
相关问题