簡體   English   中英

SwiftUI 選項卡式視圖防止滑動到下一個選項卡

[英]SwiftUI Tabbed View prevent swipe to next tab

我試圖阻止滑動到第二個選項卡,直到用戶單擊第一個選項卡式視圖上的一個按鈕,指示數據已完成。 然后用戶可以滑動。 我認為它是第一個選項卡視圖中的@State,因為它是事實的來源和主要內容視圖中的@Binding,但這不起作用。 我已經簡化了代碼並刪除了綁定信息,以便至少可以構建它。 任何想法或更好的方法表示贊賞。 (這方面很新……)

//ContentView.swift file

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()
                .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
    @State var allowSwipeTo2 = false
    @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的類型更改為tab1View @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))
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM