弹出控制器未被解雇
popover controller not being dismissed
我有这个 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)
}
弹出窗口按预期显示:
但是,当我尝试使用此方法删除单元格时:
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)
}
单元格已从超级视图中删除,对象已从核心数据中删除,但我无法关闭弹出窗口:
这两个函数在同一个控制器中声明,但是 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
}
从评论中移出:
尝试在 self
而不是 self.editAlertController
上调用 dismiss。应该是呈现弹出窗口的控制器应该将其关闭,而不是弹出窗口自行关闭。
我有这个 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)
}
弹出窗口按预期显示:
但是,当我尝试使用此方法删除单元格时:
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)
}
单元格已从超级视图中删除,对象已从核心数据中删除,但我无法关闭弹出窗口:
这两个函数在同一个控制器中声明,但是 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
}
从评论中移出:
尝试在 self
而不是 self.editAlertController
上调用 dismiss。应该是呈现弹出窗口的控制器应该将其关闭,而不是弹出窗口自行关闭。