mat mat - 1 year ago 80
iOS Question

popover controller not being dismissed

I have this

which is presented as

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:

enter image description here

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!)
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]
}catch let err {

}catch let error {


self.editAlertController?.dismiss(animated: true, completion: nil)
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:

enter image description here

The 2 functions are declared in the same controller, however
is a subclass of UIView with a protocol named
. 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)
editButton.addTarget(self, action: #selector(deleteSlot), for: .touchUpInside)


//.....more code here

func deleteSlot(){
delegate?.deleteSlot(eventCell: eventCell!, id: Int(slotid!))


//.....more code here


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download