我试图阻止滑动到第二个选项卡,直到用户单击第一个选项卡视图上的按钮,指示数据已完成.然后用户可以刷卡.我认为它在第一个选项卡视图中是@State,因为它是真理的来源,在主内容视图中是@Binding,但这不起作用.我已经简化了代码并删除了绑定信息,这样至少可以构建它.任何 idea 或更好的方法都值得赞赏.(这方面很新……)

//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))
    }
}```

推荐答案

小错误的方法.在ContentView中,应该将allowSwipeTo2变量与tab1View绑定.

为了解决您的问题,将tab1View中的变量allowSwipeTo2的类型更改为@Binding var allowSwipeTo2 : Bool,最后在ContentView中,像这样调用第一个选项卡视图tab1View(allowSwipeTo2: $allowSwipeTo2)

我修改了代码如下.您可以找到带有此注释//modified的修改部分.

struct ContentView: View {

@State private var selection = 0
@State var allowSwipeTo2 = false

var body: some View {
    VStack {
        Text("Main Content View")
            .font(.system(size: 18))
    }
    
    TabView(selection: $selection){
        tab1View(allowSwipeTo2: $allowSwipeTo2) //modified
            .tag(0)

        if allowSwipeTo2 == true {
            tab2View()
                .tag(1)
        }
        }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
}
}


struct tab1View: View {
@State private var p8_10 = 0
@State private var stepperDisabled = false
@Binding var allowSwipeTo2 : Bool //modified
@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()
        }
    }
}
}



struct tab2View: View {
var body: some View {
    Text("Tab 2 Content!")
        .font(.system(size: 15))
}
}

Ios相关问答推荐

UIBezierPath包含:方法不检测到厚度大于1像素的线上的touch

SWIFT根据地球坐标修改 node 旋转

如何创建装饰视图?

如何使用 pushViewController 从 UIHostingController 的 SwiftUI 视图中推送 ViewController?

FileHandle 不会在 iOS 中释放内存

swiftUI中的剪辑形状无法使用旋转效果

SwiftUI DatePicker 格式在每个设备上都不一样,如何让它总是显示相同的 Select 版本?

为什么我不能让 passthrough 科目为 combine 工作?

在使用 Github Actions 时,我面临权限被拒绝错误

分析应用程序版本时出错 - Apple 提交到store

Flutter:无法构建 iOS 应用程序ARCHIVE FAILED,为设备归档时遇到错误

本月使用 iOS 的天数?

UIButton在iOS7中没有显示突出显示

'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据

Xcode 故事板:内部错误.请提交错误

如何在 iOS 中判断暗模式?

aps-environment 始终在发展

判断密钥是否存在于 NSDictionary 中

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?

在 NSUserDefaults 中存储值是否有任何限制?