ios - 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
}
}
}}
解决方案
在 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)
}
}
}
推荐阅读
- c# - 将通过反射发现的类型与 .NET 中的引用类型进行比较
- xcode - Xcode:集合视图未正确显示
- javascript - 在保持引用相等的同时在 react 中传播 props
- css - 需要将 CSS 转换为所需格式的 SCSS
- html - 在 Angular 中构建网格时如何提高性能?
- excel - VBA中的分隔符,宏
- python - Python keyboard.record 不会停止录制
- python - 无法映射 1024 字节 - 无法分配内存 - 即使内存足够多
- google-api - chrome.alarms 未按预期触发的故障排除
- asp.net - 如何在 ASP.NET 中将 configSource 与 configBuilders 一起使用?