I'm trying to prevent swiping to the second tab until the user has clicked a button on the first tabbed view indicating the data is complete. Then the user can swipe. I thought it was @State in the first tab view since it's the source of truth and a @Binding in the main content view, but that didn't work. I've simplified the code and removed the Binding info so that at least it will build. Any thoughts or better approach appreciated. (Quite new at this...)
//ContentView.swift file
struct ContentView: View {
@State private var selection = 0
@State var allowSwipeTo2 = false
var body: some View {
VStack {
Text("Main Content View")
Text(String(allowSwipeTo2)) //added this line, and can see it change from false to true when button is clicked, but still not swipable
.font(.system(size: 18))
}
TabView(selection: $selection){
tab1View(allowSwipeTo2: $allowSwipeTo2) //modified based on recommendation
.tag(0)
if allowSwipeTo2 == true {
tab2View()
.tag(1)
}
}.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}
//tab1View.swift file with the button and the booleans for disabling further changes and ok to swipe:
struct tab1View: View {
@State private var p8_10 = 0
@State private var stepperDisabled = false
@Binding var allowSwipeTo2: Bool //modified based on recommendation
@State private var showingAlert = false
var body: some View {
VStack{
HStack{
Text("Things")
Stepper("Total: \(p8_10)", value: $p8_10, in: 0...15)
}.font(.system(size: 15))
.disabled(stepperDisabled)
HStack{
Spacer()
Button("Entry Complete") {
showingAlert = true
}.alert(isPresented: $showingAlert){
Alert(
title: Text("Entry Complete?"),
message: Text("You will no longer be able to change your entry"),
primaryButton: .cancel(),
secondaryButton: .destructive(
Text("OK"),
action:{
stepperDisabled = true
allowSwipeTo2 = true
})
)
}
Spacer()
}
}
}
}
//tab2View.swift file
struct tab2View: View {
var body: some View {
Text("Tab 2 Content!")
.font(.system(size: 15))
}
}```