我有这个功能 func deleteDayTracker(at indexPath: IndexPath) ,我想在alertAction中调用它,因此当按下警报操作按钮时,会调用deleteDayTracker函数及其中的代码。我尝试了 ViewController.deleteDayTracker(self) ,但随后收到错误“表达式解析为未使用的函数”。如何在alertAction中调用deleteDayTracker? 这是完整的代码
func deleteDayTracker(at indexPath: IndexPath)
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.delete(numberOfDays[indexPath.row])
context.delete(eventName[indexPath.row])
context.delete(eventDate[indexPath.row])
do
{
try context.save()
}
catch
{
let alert = UIAlertController(title: nil, message: "Unable to delete event", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okButton)
self.present(alert, animated: true, completion: nil)
}
tableView.reloadData()
}
@IBAction func editButton(_ sender: Any)
{
let editAlert = UIAlertController(title: nil, message: "Delete this day tracker?", preferredStyle: .actionSheet)
let deleteButton = UIAlertAction(title: "Delete", style: .destructive, handler: self.deleteCell)
let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
editAlert.addAction(deleteButton)
editAlert.addAction(cancelButton)
if let alert = editAlert.popoverPresentationController
{
alert.sourceView = self.view
alert.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
alert.permittedArrowDirections = []
}
self.present(editAlert, animated: true, completion: nil)
}
func deleteCell(alert: UIAlertAction!)
{
ViewController.deleteDayTracker(self)
}
最佳答案
您使用参数“at indexPath”创建了类型为“IndexPath”的方法 例如,您应该使用“根据参数”调用方法。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
self.deleteDayTracker(at: indexPath)
}
}
关于swift - 表达式解析为具有特殊函数的未使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50956994/