I have this UIViewController
which is presented as .popover
.
func editSlotPopOver(eventCell:EventCell, gr:UITapGestureRecognizer){
let location = gr.location(in: eventCell)
let editAlertController = UIViewController()
let viewAlert = EditSlotPopOver(frame: CGRect(x: 0, y: 0, width:editAlertController.view.bounds.width , height: editAlertController.view.bounds.height))
viewAlert.delegate = self
viewAlert.setEvent(event: eventCell.event!, cell: eventCell)
editAlertController.view = viewAlert
editAlertController.modalPresentationStyle = .popover
let popoverMenuViewController:UIPopoverPresentationController = editAlertController.popoverPresentationController!
popoverMenuViewController.permittedArrowDirections = .any
editAlertController.popoverPresentationController?.delegate = self
popoverMenuViewController.sourceView = eventCell
popoverMenuViewController.sourceRect = CGRect(
x: location.x,
y: location.y,
width: 1,
height: 1)
present(editAlertController, animated: true, completion: nil)
}
The popover is presented as expected:
However, when I try to delete the cell with this method:
func deleteSlot(eventCell: EventCell, id: Int){
let application = UIApplication.shared.delegate as! AppDelegate
let id:Int32 = Int32(eventCell.eventId!)
print(id)
let context = application.managedObjectContext
let fetchRequest:NSFetchRequest<Slot> = NSFetchRequest(entityName: "Slot")
fetchRequest.predicate = NSPredicate(format: "event_id = %d", id)
do {
let result = try context.fetch(fetchRequest)
let slot = result[0]
application.managedObjectContext.delete(slot)
do{
try context.save()
}catch let err {
print(err)
}
}catch let error {
print(error)
}
//DISMISS DOESN'T WORK HERE
self.editAlertController?.dismiss(animated: true, completion: nil)
eventCell.removeFromSuperview()
self.calendarView.forceReload(reloadEvent: true)
}
The cell is removed from the superview and the objected removed from core data but I am not able to dismiss the popover:
The 2 functions are declared in the same controller, however EditSlorPopOver
is a subclass of UIView with a protocol named EditSlotDelegate
. The delegate is being called but the popover is not being dismissed.
protocol EditSlotDelegate {
func deleteSlot(eventCell: EventCell, id: Int)
}
class EditSlotPopOver: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setUpEditButton()
editButton.addTarget(self, action: #selector(deleteSlot), for: .touchUpInside)
}
//.....more code here
func deleteSlot(){
delegate?.deleteSlot(eventCell: eventCell!, id: Int(slotid!))
}
//.....more code here
}
Moved from a comment:
Try calling dismiss on self
rather than the self.editAlertController
. It should be the controller thats presenting the popover that should dismiss it, not the popover dismissing itself.