首页 > 解决方案 > 我需要为我的预览调用添加一个参数。我应该添加什么而不说明布尔值是真还是假?

问题描述

我需要为我的预览调用添加一个参数。我应该添加什么而不说明布尔值是真还是假?

import SwiftUI
struct LiberteIDCreation: View {
    @Binding var LoginSucess: Bool
    var body: some View {
        VStack {
            return Group {
                if LoginSucess  {
                    ContentView()
                } else {
                    LiberteIDLogin(LoginSucess: self.LoginSucess)
                }
                }
            }
        }
}

struct LiberteIDCreation_Previews: PreviewProvider {
   static var previews: some View {
        LiberteIDCreation(LoginSucess: Bool) //what should I add for this? I need to add an agrument for this 
    }
}

我可以向 LiberteLDCreation 添加什么以使其正常工作?它说它需要一个论点?

标签: iosswiftswiftui

解决方案


答案是:你应该使用 .constant(false)in LiberteIDCreation_Previews。但这是您需要知道的另一个问题。在您的情况下(使用语句),它在画布中无法正常工作if...else,视图不会更新。至少在我的 Xcode 版本中它不起作用。试试下面的例子来检查。:

struct UsingBindingBool: View {

    @Binding var loginSuccess: Bool
    var body: some View {

        VStack {
            return Group {
                if loginSuccess  {
                    Text("success")
                } else {
                    Text("change variable from child").onTapGesture { self.loginSuccess = true }
                }

                Text("other group elements...")
            }
        }

    }
}

struct UsingStateForBinding: View {

    @State private var loginSuccess = false
    var body: some View {

        VStack {
            UsingBindingBool(loginSuccess: $loginSuccess)
                .padding(.bottom)
            Text("presenting parent button:").foregroundColor(.red)
            Button(action: {self.loginSuccess = true}) { Text("change to true from parent") }
        }


    }

}

struct UsingBindingBool_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            UsingBindingBool(loginSuccess: .constant(false))
            UsingStateForBinding()
        }

    }
}

推荐阅读