【发布时间】:2016-09-24 16:17:08
【问题描述】:
我有表格视图,并且在每个单元格中都有一个打开操作表的按钮。一旦该操作表打开,就会有一个操作“报告进度”。当我按下该操作按钮时,我想打开另一个具有 segue 标识符“ShowProgressReport”的视图控制器。在那个新的视图控制器中,我有一个默认为空的属性“ProjectName”。我希望该属性从以前的视图控制器中获取值。但我无法在“prepareForSegue”方法中获取索引值。这是我编写的代码:
表格视图
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!
}
}
有什么帮助吗?
【问题讨论】:
-
将索引保留为成员变量,并将值从
projectActions方法存储到它 -
我在@IBAction 方法中存储了像“let index = sender.tag”这样的索引值。我还需要写什么才能使其成为成员变量?
-
完成,感谢 Midhun MP
标签: ios swift xcode uistoryboardsegue