popover控制器没有被解雇

时间:2016-10-27 09:37:41

标签: ios swift uipopovercontroller

我有UIViewController,其显示为.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)

}

弹出框按预期显示:

enter image description here

但是,当我尝试使用此方法删除单元格时:

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)
}

从superview中移除了单元格,并且从核心数据中移除了对象,但我无法解除popover:

enter image description here

2个函数在同一个控制器中声明,但EditSlorPopOver是UIView的子类,其协议名为EditSlotDelegate。该代表正在被召唤,但是弹出没有被解雇。

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 


}

1 个答案:

答案 0 :(得分:0)

从评论中移出:

尝试在self而不是self.editAlertController上调用解雇。它应该是呈现弹出窗口的控制器,它应该忽略它,而不是popover解雇它。