首页 > 解决方案 > SwiftUI EKEventEditViewController 字段不可编辑

问题描述

我一直在尝试将 EKEventEditViewController 与 SwiftUI 一起使用。我尝试遵循开发论坛的一些建议,了解如何在带有协调器的 UIViewControllerRepresentable 包装器的帮助下添加它。我在下面添加代码。

EKEventEditViewController 显示正确,但我面临的问题是只有一些字段是可编辑的。我附上了一个显示交互的 gif。

有没有人遇到过这个问题?这是代码:

import Foundation
import SwiftUI
import EventKitUI

let eventStore = EKEventStore()

struct NewEventGenerator: UIViewControllerRepresentable {

typealias UIViewControllerType = EKEventEditViewController

@Binding var isShowing: Bool

var theEvent = EKEvent.init(eventStore: eventStore)

func makeUIViewController(context: UIViewControllerRepresentableContext<NewEventGenerator>) -> EKEventEditViewController {

    let controller = EKEventEditViewController()
    controller.event = theEvent
    controller.eventStore = eventStore
    controller.editViewDelegate = context.coordinator

    return controller
}

func updateUIViewController(_ uiViewController: NewEventGenerator.UIViewControllerType, context: UIViewControllerRepresentableContext<NewEventGenerator>) {
    uiViewController.view.backgroundColor = .red
}


func makeCoordinator() -> Coordinator {
    return Coordinator(isShowing: $isShowing)
}

class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {

    @Binding var isVisible: Bool

    init(isShowing: Binding<Bool>) {
        _isVisible = isShowing
    }

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        switch action {
        case .canceled:
            isVisible = false
        case .saved:
            do {
                try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
            }
            catch {
                print("Event couldn't be created")
            }
            isVisible = false
        case .deleted:
            isVisible = false
        @unknown default:
            isVisible = false
        }
    }
}}

这是录音

标签: ioscalendarswiftuieventkit

解决方案


在 Xcode 12 / iOS 14 上运行良好。从字面上复制粘贴您的代码,在 Info.plist 中添加了 requestAccess 和描述。

演示

完整测试的模块,如果有帮助的话。

import SwiftUI
import EventKitUI

let eventStore = EKEventStore()

struct NewEventGenerator: UIViewControllerRepresentable {
    typealias UIViewControllerType = EKEventEditViewController

    @Binding var isShowing: Bool
    var theEvent: EKEvent

    init(isShowing: Binding<Bool>) {
        eventStore.requestAccess(to: .event) { allow, error in
            print("Result: \(allow) or [\(error.debugDescription)]")
        }

        theEvent = EKEvent.init(eventStore: eventStore)

        _isShowing = isShowing
    }


func makeUIViewController(context: UIViewControllerRepresentableContext<NewEventGenerator>) -> EKEventEditViewController {

    let controller = EKEventEditViewController()
    controller.event = theEvent
    controller.eventStore = eventStore
    controller.editViewDelegate = context.coordinator

    return controller
}

func updateUIViewController(_ uiViewController: NewEventGenerator.UIViewControllerType, context: UIViewControllerRepresentableContext<NewEventGenerator>) {
    uiViewController.view.backgroundColor = .red
}


func makeCoordinator() -> Coordinator {
    return Coordinator(isShowing: $isShowing)
}

class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {

    @Binding var isVisible: Bool

    init(isShowing: Binding<Bool>) {
        _isVisible = isShowing
    }

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        switch action {
        case .canceled:
            isVisible = false
        case .saved:
            do {
                try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
            }
            catch {
                print("Event couldn't be created")
            }
            isVisible = false
        case .deleted:
            isVisible = false
        @unknown default:
            isVisible = false
        }
    }
}}

struct TestEventKitViewInSheet: View {     // just created in ContentView body
    @State private var showIt = false
    var body: some View {
        Button("Events") { showIt = true }
            .sheet(isPresented: $showIt) {
                NewEventGenerator(isShowing: $showIt)
            }
    }
}

推荐阅读