Swift(Xcode) - 从表格单元格向下一个控制器传递值 - >动作按钮 - > SEGUE

时间:2016-09-24 06:59:07

标签: ios swift xcode uistoryboardsegue

我有表视图,每个单元格中都有一个按钮,可以打开一个操作表。该操作表打开后,会出现“报告进度”操作。当我按下该动作按钮时,我想打开另一个具有segue标识符“ShowProgressReport”的视图控制器。在那个新的视图控制器中,我有一个属性“ProjectName”,默认为空。我希望该属性从以前的视图控制器获取值。但是我无法在“prepareForSegue”方法中获得索引值。这是我编码的内容:

的TableView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ProjectTableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("ProjectViewCell", forIndexPath:indexPath) as! ProjectTableViewCell

        // Remove indenting of cell
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero

        // Set project name
        cell.ProjectName.text = self.ProjectsArray[indexPath.row] as? String

        // Set action button
        cell.ActionButton.tag = indexPath.row
        cell.ActionButton.addTarget(self, action: #selector(ProjectsController.projectActions(_:)), forControlEvents: .TouchUpInside)

        return cell

    }

按钮操作表

@IBAction func projectActions(sender: UIButton) {
        let index = sender.tag

        let optionMenu = UIAlertController(title: nil, message: self.ProjectsArray[index] as? String, preferredStyle: .ActionSheet)

        // Report Progress
        let reportProgressAction = UIAlertAction(title: "Report Progress", style: .Default, handler: {
            (alert: UIAlertAction!) -> Void in
            self.performSegueWithIdentifier("ShowReportProgress", sender: self)
        })

        // Cancel
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (alert: UIAlertAction!) -> Void in

        })

        optionMenu.addAction(reportProgressAction)
        optionMenu.addAction(cancelAction)

        self.presentViewController(optionMenu, animated: true, completion: nil)
    }

执行Segue

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

        let index = sender?.tag

        if segue.identifier == "ShowReportProgress"
        {
            let upcoming: ReportProgressController = segue.destinationViewController as! ReportProgressController

            let ProjectName = self.ProjectsArray[index!] as? String --> Here it says: fatal error: unexpectedly found nil while unwrapping an Optional value
            upcoming.ProjectName = ProjectName!
        }

    }

任何帮助?

2 个答案:

答案 0 :(得分:2)

在这一行

self.performSegueWithIdentifier("ShowReportProgress", sender: self)

您正在传递代表视图控制器的self。你可能想要传递按钮。

self.performSegueWithIdentifier("ShowReportProgress", sender: sender)

你应该总是使用可选的绑定来避免崩溃和解包保证存在的值。

if let button = sender where segue.identifier == "ShowReportProgress" {
    let upcoming: ReportProgressController = segue.destinationViewController as! ReportProgressController

    let ProjectName = self.ProjectsArray[button.tag] as! String
    upcoming.ProjectName = ProjectName
}

答案 1 :(得分:-1)

替换

self.performSegueWithIdentifier("ShowReportProgress", sender: self)

。通过

self.performSegueWithIdentifier("ShowReportProgress", sender: sender)

您正在performSegueWithIdentifier传递控制器参考而不是按钮参考。