【问题标题】:SwiftUI View doesn't change when using button and @State / @Binding使用按钮和@State / @Binding 时,SwiftUI 视图不会改变
【发布时间】:2021-08-18 02:35:59
【问题描述】:

长话短说,这是用户通过身份验证后的入职视图。我的应用程序的主要导航使用navigationview,但我不能将其用于入职。我已经在主屏幕上放了一个全屏封面来显示这些入门内容。

但是,当尝试在入门屏幕之间的不同文件的视图之间简单导航时,该按钮不会显示其他视图。我已经尝试了@Appstorage 和@Environment 的所有东西,但无法让它工作。

另外请注意,我切断了文件其余部分的底部,因为这与此逻辑无关。

import SwiftUI

struct OnboardingTestView: View {
    @State var shouldShowOnboarding = true
    var body: some View {
        if shouldShowOnboarding {
            OffsetChoicesView(shouldShowOnboarding: $shouldShowOnboarding)
        }
        if shouldShowOnboarding == false {
            PersonalInfoView()
        }
    }
}

struct OnboardingTestView_Previews: PreviewProvider {
    static var previews: some View {
        OnboardingTestView()
    }
}

//*********************************************************************
//OffsetChoicesView

struct OffsetChoicesView: View {
    @Binding var shouldShowOnboarding: Bool
    var body: some View {
        
        ZStack {
            Color(#colorLiteral(red: 0.9803921569, green: 0.9568627451, blue: 0.9568627451, alpha: 1)).edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            
            VStack {

            Progress1View()
                .padding(.bottom, 40)
                .padding(.top)
            
            Spacer()
                Button(action: {
                    shouldShowOnboarding = false
                }) {
                    NextButtonView()
                        .padding(.top)
                        .padding(.bottom)
                }
            
            }

【问题讨论】:

  • 代替if shouldShowOnboarding == false { 试试else {
  • 另外,尝试将 if 语句嵌入到 VStackGroup
  • 此代码在NextButtonView 按钮上向我显示PersonalInfoView。 p.s.即使您使用的是fullScreenCover,您也可以在其中再声明一个NavigationView,然后毫无问题地使用NavigationLink

标签: swiftui swiftui-environment


【解决方案1】:

你可以尝试这样的事情 - 利用@AppStorage

我不太确定 OffsetChoiceView() 参数应该是什么......所以我只是在示例中删除了它们。这应该是您的入职视图。

    import SwiftUI

struct OnboardingTestView: View {
    //@State var shouldShowOnboarding = true
    
    @AppStorage("showOnboarding") var showOnboarding: Bool = true
    var body: some View {
        if (showOnboarding == true) {
            OffsetChoicesView()
        } else if (showOnboarding == false) {
            PersonalInfoView()
        }
    }
}

struct OnboardingTestView_Previews: PreviewProvider {
    static var previews: some View {
        OnboardingTestView()
    }
}

//*********************************************************************
//OffsetChoicesView

struct OffsetChoicesView: View {
    //@Binding var shouldShowOnboarding: Bool
    @AppStorage("showOnboarding") var showOnboarding: Bool = false
    var body: some View {
        
        ZStack {
            Color(#colorLiteral(red: 0.9803921569, green: 0.9568627451, blue: 0.9568627451, alpha: 1)).edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            
            VStack {

            Progress1View()
                .padding(.bottom, 40)
                .padding(.top)
            
            Spacer()
                Button(action: {
                    showOnboarding = false
                }) {
                    NextButtonView()
                        .padding(.top)
                        .padding(.bottom)
                }
            
            }

【讨论】:

  • 你不能用BoolAppStorage 吗?
  • 是的,你应该可以。尝试将 AppStorage 更改为以下内容(在移动设备上,所以最好的猜测):@AppStorage("showOnboarding") var showOnboarding: Bool = false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
相关资源
最近更新 更多