ios - Swift (Xcode) - 从表格单元格传递值到下一个 Controller -> Action 按钮 -> Segue

标签 ios swift xcode uistoryboardsegue

我有表格 View ,在每个单元格中都有一个按钮可以打开一个操作表。一旦该操作表打开,就会出现“报告进度”操作。当我按下该操作按钮时,我想打开另一个具有 segue 标识符“ShowProgressReport”的 View Controller 。在那个新的 View Controller 中,我有一个属性“ProjectName”,默认情况下它是空的。我希望该属性从以前的 View Controller 中获取值。但是我无法在“prepareForSegue”方法中获取索引值。这是我编写的代码:

表格 View

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)
    }

执行转场

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!
        }

    }

有什么帮助吗?

最佳答案

在这一行

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

您正在传递代 TableView Controller 的 self。您可能是想通过按钮。

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

您应该始终使用可选绑定(bind)来避免崩溃和解包保证存在的值。

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
}

关于ios - Swift (Xcode) - 从表格单元格传递值到下一个 Controller -> Action 按钮 -> Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673512/

相关文章:

iOS 7 - 在自定义 tableViewCell 中更改 textField 的键盘类型

json - 快速解码嵌套的 JSON

ios - 我将如何显示一组注释?

ios - 所有 UILabel 的默认文本颜色

ios - 如何在不子类化的情况下使其他类可以访问变量?

iOS 应用程序 - iOS 11.4 随机崩溃

ios - 警告 : Output of vertex shader 'v_gradient' not read by fragment shader

iphone - 出现键盘时调整 View 大小 (iOS)

xcode - 代码从 2.0 到 1.0 的转换

ios - 在 SWIFT 中为 iOS 开发解析 json 的问题