gpt4 book ai didi

关闭 .fullScreenCover 或 .sheet 后的 SwiftUI Picker 问题

转载 作者:行者123 更新时间:2023-12-04 14:47:48 27 4
gpt4 key购买 nike

我有一个在显示和关闭 fullScreenCover 或工作表之前工作正常的选择器。有谁知道这个示例代码有什么问题,或者有解决方法?

我也尝试使用 self.presentation.wrappedValue.dismiss() 关闭工作表,但结果相同。

示例 gif:/image/zmcmv.gif

代码:

import SwiftUI

struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail = false

var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail.toggle()
}, label: {
Text("Detail popup")
})

Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}
}
.fullScreenCover(isPresented: $showDetail, content: {
MyDetailsView(presenting: $showDetail)
})
}
.navigationTitle("Main")
}
}

struct MyDetailsView: View {
@Binding var presenting: Bool

var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
presenting.toggle()
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}

enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive

var id: String { self.rawValue }
}

extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"

}
}
}

最佳答案

我完全同意系统中存在错误。但是,您可以绕过它。这是对我有用的解决方法,已在 ios-15 和 macCatalyst (macos12.01) 设备上测试:

import SwiftUI

@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail: ActiveStatus? // <-- here

var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail = ActiveStatus.active // <-- here
}, label: { Text("Detail popup") })

Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}.pickerStyle(.menu)

}
// -- here --
.fullScreenCover(item: $showDetail) { _ in
MyDetailsView()
}
}
.navigationViewStyle(.stack)
.navigationTitle("Main")
}
}

struct MyDetailsView: View {
@Environment(\.dismiss) var dismiss // <-- here

var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
dismiss() // <-- here
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}

enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive

var id: String { self.rawValue }
}

extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}

关于关闭 .fullScreenCover 或 .sheet 后的 SwiftUI Picker 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69697944/

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