'loginpage.Type'不能转换为'(Binding <Bool>)-> loginpage'

时间:2019-12-04 18:04:08

标签: boolean swiftui

我正在尝试将用户重定向到登录页面,他们可以在其中使用firebase登录,但出现上述错误。

在他们输入正确的用户名和密码后,我也使用相同的代码(但是更改了)来重定向它们。

首页

import SwiftUI

struct onboarding1: View {

    @Binding var isOnboardingDone: Bool

    init(_ isOnboardingDone: Binding<Bool>) {
        self._isOnboardingDone = isOnboardingDone
    }

    var body: some View {
        NavigationView {
        ZStack {
            Color("background").edgesIgnoringSafeArea(.all)
        VStack {
            Image("logo")

            Button(action: {
                self.isOnboardingDone.toggle()
            }) {
                Text("toggle")
            }
                }
            }
        }
    }
}
struct shmuck_Previews: PreviewProvider {
    static var previews: some View {
        onboarding1(.constant(false))
    }
}

其他页面

import SwiftUI

struct supporter: View {

   @State var isOnboardingDone = false

        var body: some View {
            Group {
            if isOnboardingDone {
               loginpage()  // error displayed here ('loginpage.Type' is not convertible to '(Binding<Bool>) -> loginpage')
            } else {
                onboarding1($isOnboardingDone)
            }
        }
    }
}

struct supporter_Previews: PreviewProvider {
    static var previews: some View {
        supporter()
    }
}

此处要求的是登录页面代码

登录页面

import SwiftUI
import Firebase

struct loginpage: View {

    @Binding var sos: Bool

       init(_ sos: Binding<Bool>) {
           self._sos = sos
       }

    @State var email = ""
    @State var password = ""
    var body: some View {
      NavigationView {

            ZStack {
                Color("background").edgesIgnoringSafeArea(.all)
            VStack {
                Image("fulllogo")
                    .offset(y: -100)
            Text("username")
                .offset(y: -200)
               TextField("Enter your email", text: $email)
                .offset(y: -200)
            Text("Password")
.offset(y: -200)
            TextField("Enter your password", text: $password)
          .offset(y: -200)
                Button(action: {
                    Auth.auth().signIn(withEmail: self.email, password: self.password) { (result, error) in
                        if error != nil {
                        print(error?.localizedDescription)
                    } else {
                    // user signed in
                        self.sos.toggle()
                    }
                    }
                }) {
                    RoundedRectangle(cornerRadius: 10)
                    .frame(width: 300, height: 40)
                    .overlay(
                    Text("Let's Go!")
                        .foregroundColor(.white)
                    )
                }.offset(y: -180)
            }.navigationBarTitle("Login")

                }
            }
        }
    }

struct heyMama_Previews: PreviewProvider {
    static var previews: some View {
        loginpage(.constant(false))
    }
}

其他页面

import SwiftUI

struct login2: View {
    @State var sos = false
    var body: some View {

                    Group {
            if sos {
                LoggedIn()
            } else {
                loginpage($sos)
            }
        }

    }
}

struct login2_Previews: PreviewProvider {
    static var previews: some View {
        login2()
    }
}

2 个答案:

答案 0 :(得分:1)

@Binding var sos: Bool中使用init(_ sos: Binding<Bool>)方法声明了loginpage并对其进行了初始化之后,必须将绑定变量作为参数传递。

您需要传递绑定变量,例如@State。您可以参考this answer了解有关@Binding

的更多详细信息

尝试以下代码。

struct supporter: View {

   @State var isOnboardingDone = false
   @State var sos = false

        var body: some View {
            Group {
            if isOnboardingDone {
               loginpage(sos: $sos)  
            } else {
                onboarding1($isOnboardingDone)
            }
        }
    }
}

因此,只要您将sos变量的值从loginpage更改,它也会反映在supporter页面上。

答案 1 :(得分:0)

下面的代码有效。

struct supporter: View {

   @State var isOnboardingDone = false
   @State var sos = false

        var body: some View {
            Group {
            if isOnboardingDone {
                loginpage($sos)
            } else {
                onboarding1($isOnboardingDone)
            }
        }
    }
}