我有表格視圖,並且在每個單元格中都有一個打開操作表單的按鈕。一旦該操作表打開,就會有一個「報告進度」操作。當我按下該動作按鈕時,我想打開另一個具有segue標識符「ShowProgressReport」的視圖控制器。在那個新的視圖控制器中,我有一個默認爲空的屬性「ProjectName」。我希望該屬性從前一個視圖控制器中獲取價值。但是我無法在「prepareForSegue」方法中獲得索引值。以下是我已經編碼:Swift(Xcode) - 將值傳遞給下一個控制器從表格單元格 - >動作按鈕 - > Segue
的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!
}
}
有什麼幫助嗎?
將索引保留爲成員變量並將值從'projectActions'方法存儲到它 –
我在@IBAction方法中存儲了像「let index = sender.tag」這樣的索引值。我還需要寫什麼來使其成爲一個成員變量? – Ali
完成,感謝Midhun MP – Ali