我想显示一个TabView
以显示不同的屏幕。我的第一个屏幕(主页)显示三个按钮,可以选择要显示的三个屏幕之一。但是(!)我必须检查是否重复选择了一个选项卡以在这种情况下触发特殊操作。
检测 screen2 的重复选择工作正常,但我无法通过按钮设置选择。我曾尝试使用@EnvironmentalObject
但在我的 TabView.selection 中未观察到对此对象的更改。
import SwiftUI
@main
struct TestApp: App {
static let kIndex0 = 0
static let kIndex1 = 1
static let kIndex2 = 2
var appState = AppState()
@State private var selection = TestApp.kIndex0
var body: some Scene {
// this code is required to detect a repeated selection of
// the same tab to trigger a special action
let index = Binding<Int>(
get: { self.selection },
set: {
if $0 == …
Run Code Online (Sandbox Code Playgroud)