简体   繁体   English

SwiftUI 中的条件导航从另一个视图

[英]Conditional Navigation in SwiftUI from another view

I have created separate views for buttons and textField.我为按钮和文本字段创建了单独的视图。 When I tap on the button I should check for textField is empty/not and if it is not empty, I should navigate to the next page.当我点击按钮时,我应该检查 textField 是否为空,如果它不为空,我应该导航到下一页。 Here I got struck with managing navigationDestination based on the scenario.在这里,我对基于场景管理 navigationDestination 感到震惊。

class UserProgress: ObservableObject {
    @Published var value = ""
    @Published var flag = false
 }

struct ButtonView: View {
    @StateObject var progress = UserProgress()
    
    var body: some View {
        Button("Button title") {
            print("Button tapped! \(progress.value.count)")
            progress.flag = true
        }
    }
}

struct TextView: View {
    
    @Binding var textValue: String
    @ObservedObject var progress: UserProgress
    
    var body: some View {
        VStack {
            TextField("Hello", text: $textValue)
                .onChange(of: textValue) {
                    progress.value = $0
            }
        }
    }
}

struct mainViewApp: View {
    @StateObject var progress = UserProgress()
    @State var readyToNavigate: Bool = false
    @State var text1Value = ""
    @State var text2Value = ""
    
    var body: some View {
        NavigationStack {
            ZStack {
                VStack {
                    VStack {
                        Text("Your value is \(progress.value)")
                        
                        TextView(textValue: $text1Value, progress: progress)
                        if (progress.flag && text1Value.isEmpty) {
                            Text("ERROR1")
                        }
                        TextView(textValue: $text2Value, progress: progress)
                        if (progress.flag && text2Value.isEmpty) {
                            Text("ERROR2")
                        }
                        ButtonView(progress: progress)
                    }
                }
            }
            .padding()
            .navigationBarTitle("Main Menu", displayMode: .inline)
            .navigationDestination(isPresented: $readyToNavigate) {
                Text("Hello test2")
            }
            
            .toolbarColorScheme(.dark, for: .navigationBar)
            .toolbarBackground(
                Color.black,
                for: .navigationBar)
            .toolbarBackground(.visible, for: .navigationBar)
        }
    }
}

struct test_Previews: PreviewProvider {
    static var previews: some View {
        mainViewApp()
    }
}

here I tried managing its observables but couldn't able to update the state value of readyToNavigate inside the condition where I Am checking.在这里,我尝试管理它的可观察对象,但无法在我正在检查的条件下更新 readyToNavigate 的 state 值。 Advance thanks预先感谢

This is a better way to handle the viewmodel这是处理viewmodel的更好方法

 import Foundation
        import SwiftUI

        class UserProgress: ObservableObject {
            @Published var canNavigate = false
            @Published var text1Value = ""
            @Published var text2Value = ""
            
            
            func fieldsEmpty() -> Bool {
                return !text1Value.isEmpty && !text2Value.isEmpty
            }
        }

        struct ButtonView: View {
            @ObservedObject var progress: UserProgress
            
            var body: some View {
                Button("Button title") {
                    print("Button tapped!")
                    if (progress.fieldsEmpty()) {
                        progress.canNavigate = true
                    }
                }
            }
        }

        struct TextView: View {
            
            @Binding var textValue: String
            
            var body: some View {
                VStack {
                    TextField("Hello", text: $textValue)
                }
            }
        }

        struct mainViewApp: View {
            @StateObject var progress = UserProgress()
            
            var body: some View {
                NavigationStack {
                    ZStack {
                        VStack {
                            VStack {
                                Text("Your value is \(progress.text1Value)")
                                
                                TextView(textValue: $progress.text1Value)
                                
                                TextView(textValue: $progress.text2Value)
                                
                                if(!progress.fieldsEmpty()) {
                                    Text("Please fill boths textfields")
                                        .foregroundColor(.red)
                                }
                                
                                ButtonView(progress: progress)
                            }
                        }
                    }
                    .padding()
                    .navigationBarTitle("Main Menu", displayMode: .inline)
                    .navigationDestination(isPresented: $progress.canNavigate) {
                        Text("Hello test2")
                    }
                    
                    .toolbarColorScheme(.dark, for: .navigationBar)
                    .toolbarBackground(
                        Color.black,
                        for: .navigationBar)
                    .toolbarBackground(.visible, for: .navigationBar)
                }
            }
        }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM