gpt4 book ai didi

ios - SwiftUI EKEventEditViewController 字段不可编辑

转载 作者:行者123 更新时间:2023-12-01 16:11:33 24 4
gpt4 key购买 nike

我一直在尝试将 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
}
}
}}
Here is the recording

最佳答案

与 Xcode 12/iOS 14 一起工作正常。从字面上复制粘贴您的代码,在 Info.plist 中添加 requestAccess 和描述。
demo
完整测试的模块,如果有帮助的话。

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

关于ios - SwiftUI EKEventEditViewController 字段不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63104171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com