几周前我刚刚开始使用 SwiftUI,我正在学习。今天我遇到了一个问题。
当我展示带有导航栏项目按钮的工作表然后关闭 ModalView 并返回到 ContentView 时,我发现自己无法再次单击导航栏项目按钮。
我的代码如下:
struct ContentView: View {
@State var showSheet = false
var body: some View {
NavigationView {
VStack {
Text("Test")
}.sheet(isPresented: self.$showSheet) {
ModalView()
}.navigationBarItems(trailing:
Button(action: {
self.showSheet = true
}) {
Text("SecondView")
}
)
}
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentation
var body: some View {
VStack {
Button(action: {
self.presentation.wrappedValue.dismiss()
}) {
Text("Dismiss")
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我最近一直在尝试制作一个小部件,并想在我的小部件和我的应用程序之间共享一段数据。
static var sharedDataFileURL: URL {
let appGroupIdentifier = "group.com.unknownstudios.yk"
guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)
else { preconditionFailure("Expected a valid app group container") }
return url.appendingPathComponent("userID.plist")
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在我的应用程序上运行良好,但是当我在我的小部件上运行它时,它会给出以下输出。我已检查应用程序组是否正确,并且在 iOS 应用程序和小部件上均处于活动状态。
[unspecified] container_create_or_lookup_path_for_platform: client is not entitled
[unspecified] container_create_or_lookup_app_group_path_by_app_group_identifier: client is not entitled
Fatal error: Expected a valid app group container: file WidgetExtension/Library.swift, line 143
Run Code Online (Sandbox Code Playgroud)
编辑:
我也尝试过使用 UserDefaults,但它们也不起作用。
以下是我使用FileManager和UserDefaults的方式
UserDefaults(suiteName: "group.com.unknownstudios.yk")!.set("**USERID**", forKey: "userID")
let data = Data("**USERID**".utf8)
do {
try data.write(to: URL.sharedDataFileURL, options: .atomic)
} catch {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
以下是我尝试从小部件读取数据的方法: …