简体   繁体   English

EKEventEditViewController 显示一个禁用的添加按钮

[英]EKEventEditViewController is presented with a disabled Add button

The problem问题

I'm presenting a EKEventEditViewController view with some predefined information.我正在展示一个带有一些预定义信息的EKEventEditViewController视图。 If the user doesn't want to change this information, he should be able to tap Add to add the event to the calendar.如果用户不想更改此信息,他应该能够点击添加以将事件添加到日历中。

The problem is, the Add button is disabled by default.问题是,添加按钮默认是禁用的。 It is only enabled if the user changes something (like the event name or the Calendar, for example).它仅在用户更改某些内容(例如事件名称或日历)时启用。

Snippet of code代码片段

class EventManager {

    private var eventEditViewController: EKEventEditViewController?
    private let eventStore = EKEventStore()

    func addToCalendar(_ eventData: EventData) {
        let event = createEvent(eventData)
        presentEvent(event)
    }

    private func createEvent(_ eventData: EventData) -> EKEvent {
        let event = EKEvent(eventStore: eventStore)
        event.title = "My event"
        event.startDate = Date()
        event.endDate = Date()
        event.isAllDay = true
        event.calendar = eventStore.defaultCalendarForNewEvents
        event.availability = .free
        event.addAlarm(EKAlarm.init(absoluteDate: event.startDate))
        event.url = URL(string: "http://myurl.com/")
        return event
    }

    private func presentEvent(_ event: EKEvent) {
        DispatchQueue.main.async {
            self.eventEditViewController = EKEventEditViewController()
            self.eventEditViewController!.eventStore = self.eventStore
            self.eventEditViewController!.event = event
            self.eventEditViewController!.editViewDelegate = self

            self.viewController?.present(self.eventEditViewController!, animated: true)
        }
    }

}

extension EventManager: EKEventEditViewDelegate {

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        eventEditViewController?.dismiss(animated: true, completion: {
            self.delegate.finish(result: CalendarResult.fromAction(action))
        })
    }

}

EKEventEditViewController EKEventEditViewController

Here's how the EKEventEditViewController is presented:以下是EKEventEditViewController的呈现方式:

<code>EKEventEditViewController</code> 是如何呈现的

One more thing还有一件事

Another thing I've noticed is that when I remove the start and end date from my EKEvent object, the Add button is enabled by default.我注意到的另一件事是,当我从EKEvent object 中删除开始和结束日期时,默认情况下会启用添加按钮。

How can I configure my EKEvent object, in a way that it has a custom start and end date, and at the same time enable the Add button of EKEventEditViewController by default?如何配置我的EKEvent object,使其具有自定义的开始和结束日期,同时默认启用EKEventEditViewController的添加按钮?

This was fixed on iOS 13.3 beta这已在 iOS 13.3 beta 上修复

On iOS 12.2.x : Apparently the EKEventEditViewController is treating your event as an already existing event and not as a new event.在 iOS 12.2.x 上:显然EKEventEditViewController将您的事件视为已经存在的事件,而不是新事件。 Thus disabling the Add button since no changes where made (Apple bug).因此禁用“添加”按钮,因为没有进行任何更改(Apple 错误)。

A small way to prove it is to try to edit the title by removing a character, this will enable the add because now it changed from the original.证明它的一个小方法是尝试通过删除一个字符来编辑标题,这将启用添加,因为现在它与原来的不同。 If you put back the same character you just removed it will disable the Add button again.如果您放回刚刚删除的相同字符,它将再次禁用“添加”按钮。

A workaround we found was to subclass the EKEventEditViewController like this:我们发现的一种解决方法是像这样子类化EKEventEditViewController

final class FixedEventEditViewController: EKEventEditViewController {
    /// Set this variable instead of the `event` property to avoid a crash on iOS 12+ when a fixed timezone is set
    var deferredEvent: EKEvent?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let deferredEvent = self.deferredEvent {
            // Trick iOS into thinking that the event changed so it enables the Add button on iOS 13.2.x -> Fixed starting iOS 13.3
            let titleDeferred = deferredEvent.title
            deferredEvent.title = nil
            // Set the event to the new deferred event that contains no title
            self.event = deferredEvent
            // Set the original title. This will let iOS think the event changed and enable the Add button
            self.event?.title = titleDeferred
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // This is to hide the keyboard
        self.view.endEditing(true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EventKit:出现EKEventEditViewController实例时出错,然后在位置编辑后崩溃 - EventKit: error when an instance of EKEventEditViewController presented, then a crash after location edit 如何在UIModalPresentationPageSheet中显示的模态视图角添加关闭按钮? - how to add close button to modal view corner which is presented in UIModalPresentationPageSheet? iTunes Connect中的添加版本按钮已禁用 - Add version button in iTunes Connect is disabled 这是EKEventEditViewController的错误吗 - Is this a bug for EKEventEditViewController SWRevealViewController向显示的ViewController添加手势 - SWRevealViewController add gesture to a presented viewcontroller 将UINavigationBar添加到以模态方式呈现的TableViewController中? - Add a UINavigationBar to a TableViewController presented modally? 当弹出窗口时,UISplitViewController在iOS 8中禁用横向旋转的肖像 - UISplitViewController portrait to landscape rotation disabled in iOS 8 when popover presented 如何在点击“添加”或“取消”按钮后关闭EKEventEditViewController - How do I get the EKEventEditViewController to dismiss after tapping “Add” or “Cancel” buttons 导航控制器的“后退按钮”文本:以模态显示 - Navigation Controller “back button” text: Presented Modally 导航按钮问题中呈现的模态视图 - Modal view presented in navigation button issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM