0

我正在尝试在 SwiftUI 中创建一个多页表单。

我发现的问题是,一旦表格完成(或刚刚取消),似乎没有任何明确的方法能够跳回开始的“根”。

我知道回复(以及一些我不想使用的第三方库)在

SwiftUI:如何在此处弹出到 Root 视图和其他视图。

我的一些代码是基于它们的,但有些是旧答案,到处都是,似乎不起作用。

请注意,它应该适用于任何数量的表单页面。大多数答案假设有几页。

现在是 2021 年 9 月,我正在使用 XCode 12.. 和 Swift 5.. 和 iOS 14...

它大致似乎正在工作,但我遇到的第一个问题是当第一个表单输入页面打开时,任何文本输入字段都输入了内容,它会立即跳回根目录。

我可以创建一个以编程方式交换页面的视图,但这似乎有点不合时宜。(这是我目前正在开发的。即使是 Apple 员工似乎也无法对这个问题给出一个好的答案,想想你必须付给他们的所有钱,只是为了编写代码......)。

执行此操作的最新最佳实践方法是什么?

我试图从我自己的测试中创建以下样板代码:

import SwiftUI

class FormStepViewModel : ObservableObject {
   
    @Published var isActivate = false
    
    @Published var stepOneFieldOne: String = ""
    @Published var stepTwoFieldOne: String = ""
    @Published var stepThreeFieldOne: String = ""
    @Published var stepFourFieldOne: String = ""
    
}
import SwiftUI

struct RootView: View {
    
    @StateObject var formStepViewModel = FormStepViewModel()
    
    var body: some View {
    
        NavigationView {
            
                NavigationLink(destination: StepOne(), isActive: $formStepViewModel.isActivate) {
                    Text("Start Form")
                }
                
            }
            .navigationBarTitle("Welcome")
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .environmentObject(formStepViewModel)
        
    }
}
import SwiftUI

struct StepOneView: View {
    
    @EnvironmentObject var formStepViewModel: FormStepViewModel
    
    var body: some View {
        
        Form {
           
            Section(header: Text("Step One Field One")
                                .font(.headline)
                                .foregroundColor(.black)
                                .padding(EdgeInsets(top: 30, leading: 5, bottom: 0, trailing: 0))) {
                TextField("Required", text: $formStepViewModel.stepOneFieldOne)
            }
            .textCase(nil)
           
            Section {
                NavigationLink(destination: StepTwo()) {
                    Text("Next").padding().foregroundColor(Color.blue)
                }
            }
        
        }
        .navigationBarTitle("Step One")
        .navigationBarItems(trailing:
                    Button(action: {
                        self.formStepViewModel.isActivate = false  // Jump to ROOT
                    }) {
                        Text("Cancel")
                    }
        )
        
    }
}
import SwiftUI

struct StepTwoView: View {
    
    @EnvironmentObject var formStepViewModel: FormStepViewModel
    
    var body: some View {
        
        Form {
           
            Section(header: Text("Step Two Field One")
                                .font(.headline)
                                .foregroundColor(.black)
                                .padding(EdgeInsets(top: 30, leading: 5, bottom: 0, trailing: 0))) {
                TextField("Required", text: $formStepViewModel.stepTwoFieldOne)
            }
            .textCase(nil)
           
            Section {
                NavigationLink(destination: StepThree()) {
                    Text("Next").padding().foregroundColor(Color.blue)
                }
            }
        
        }
        .navigationBarTitle("Step Two")
        .navigationBarItems(trailing:
                    Button(action: {
                        self.formStepViewModel.isActivate = false  // Jump to ROOT
                    }) {
                        Text("Cancel")
                    }
        )
        
    }
}
import SwiftUI

struct StepThreeView: View {
    
    @EnvironmentObject var formStepViewModel: FormStepViewModel
    
    var body: some View {
        
        Form {
           
            Section(header: Text("Step Three Field One")
                                .font(.headline)
                                .foregroundColor(.black)
                                .padding(EdgeInsets(top: 30, leading: 5, bottom: 0, trailing: 0))) {
                TextField("Required", text: $formStepViewModel.stepThreeFieldOne)
            }
            .textCase(nil)
           
            Section {
                NavigationLink(destination: StepFour()) {
                    Text("Next").padding().foregroundColor(Color.blue)
                }
            }
        
        }
        .navigationBarTitle("Step Three")
        .navigationBarItems(trailing:
                    Button(action: {
                        self.formStepViewModel.isActivate = false  // Jump to ROOT
                    }) {
                        Text("Cancel")
                    }
        )
        
    }
}
import SwiftUI

struct StepFourView: View {
    
    @EnvironmentObject var formStepViewModel: FormStepViewModel
    
    var body: some View {
        
        Form {
           
            Section(header: Text("Step Four Field One")
                                .font(.headline)
                                .foregroundColor(.black)
                                .padding(EdgeInsets(top: 30, leading: 5, bottom: 0, trailing: 0))) {
                TextField("Required", text: $formStepViewModel.stepFourFieldOne)
            }
            .textCase(nil)
           
            Button(action: {
                        // DO_STUFF
                        self.formStepViewModel.isActivate = false  // Jump to ROOT
                    }) {
                        Text("DO_STUFF_AND_POP_TO_ROOT")
                    }
        
        }
        .navigationBarTitle("Step Four")
        .navigationBarItems(trailing:
                    Button(action: {
                        self.formStepViewModel.isActivate = false  // Jump to ROOT
                    }) {
                        Text("Cancel")
                    }
        )
        
    }
}
4

0 回答 0