我有这个功能
func deleteDayTracker(at indexPath: IndexPath)
,我想在alertAction中调用它,因此当按下警报操作按钮时,将调用deleteDayTracker函数并在其中执行代码。我尝试了ViewController.deleteDayTracker(self)
,但随后出现错误Expression解析为未使用的函数。如何在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)
}
答案 0 :(得分:0)
您使用参数“ at indexPath”创建了类型为“ IndexPath”的方法 例如,您应该使用根据参数调用方法。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
self.deleteDayTracker(at: indexPath)
}
}