我有一个应用程序可以使用 Swift 中的 EKEventEditViewController 向用户日历添加事件。一切都像它应该的那样工作,除了 editViewController 在单击导航栏中的一个按钮后不会关闭。 “添加”按钮确实保存了事件,我只是无法返回到另一个 View 。
这是我在按钮操作中的代码。我正在从之前在应用程序中创建的对象中提取数据。
@IBAction func addToCalendar(sender: AnyObject) {
var eventController = EKEventEditViewController()
var editViewDelegate: EKEventEditViewDelegate!
var store = EKEventStore()
eventController.eventStore = store
eventController.editViewDelegate = editViewDelegate
self.dismissViewControllerAnimated(true, completion: nil)
var event = EKEvent(eventStore: store)
event.title = currentEvent?.name
event.startDate = currentEvent?.startDate
event.endDate = currentEvent?.endDate
eventController.event = event
var status = EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent)
switch status {
case .Authorized:
//self.setNavBarAppearanceStandard()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.presentViewController(eventController, animated: true, completion: nil)
})
case .NotDetermined:
store.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted, error) -> Void in
if granted == true {
//self.setNavBarAppearanceStandard()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.presentViewController(eventController, animated: true, completion: nil)
})
}
})
case .Denied, .Restricted:
UIAlertView(title: "Access Denied", message: "Permission is needed to access the calendar. Go to Settings > Privacy > Calendars to allow access for the Be Collective app.", delegate: nil, cancelButtonTitle: "OK").show()
return
}
}
我从另一个 stackoverflow 问题中得到这段代码 here并且除了 self.setNavBarAppearanceStandard()
行之外的所有内容都正常工作,所以我提交了它们。
所以在这方面的任何帮助都会很棒。
最佳答案
您的委托(delegate)设置不正确。 完全删除“var editViewDelegate”。 添加
EKEventEditViewDelegate
到你的类(在 UIViewController 之后添加一个“,”并输入它)并编写这个函数:
func eventEditViewController(controller: EKEventEditViewController,
didCompleteWithAction action: EKEventEditViewAction){
self.dismissViewControllerAnimated(true, completion: nil)
}
这是委托(delegate)的协议(protocol)函数。 最后,更改一下:
eventController.editViewDelegate = editViewDelegate
到:
eventController.editViewDelegate = self
就是这样!
关于ios - 如何在点击 "Add"或 "Cancel"按钮后关闭 EKEventEditViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31193517/