Swift (Xcode) - 从 Table Cell -> Action Button -> Segue 将值传递给下一个控制器
Swift (Xcode) - Pass Value to Next Controller From Table Cell -> Action Button -> Segue
我有 table 视图并且在每个单元格中都有一个按钮可以打开一个操作 sheet。一旦该操作 sheet 打开,就会有一个操作 "Report Progress"。当我按下那个动作按钮时,我想打开另一个具有 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
}
按钮动作Sheet
@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!
}
}
有什么帮助吗?
在这一行
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
}
替换这个
self.performSegueWithIdentifier("ShowReportProgress", sender: self)
与
self.performSegueWithIdentifier("ShowReportProgress", sender: sender)
您在 performSegueWithIdentifier
中传递控制器引用而不是按钮引用。
我有 table 视图并且在每个单元格中都有一个按钮可以打开一个操作 sheet。一旦该操作 sheet 打开,就会有一个操作 "Report Progress"。当我按下那个动作按钮时,我想打开另一个具有 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
}
按钮动作Sheet
@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!
}
}
有什么帮助吗?
在这一行
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
}
替换这个
self.performSegueWithIdentifier("ShowReportProgress", sender: self)
与
self.performSegueWithIdentifier("ShowReportProgress", sender: sender)
您在 performSegueWithIdentifier
中传递控制器引用而不是按钮引用。