我有表格 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/