如何在SwiftUI中初始化Binding:Bool变量?

时间:2020-03-28 18:03:16

标签: swift swiftui init

如何初始化shouldPopToRootView?这是我的代码:

import SwiftUI

struct DoctorHomePage: View {

    @Binding var shouldPopToRootView : Bool

    init() {
        UINavigationBar.appearance().backgroundColor = .clear
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    }

    var body: some View {
        NavigationView {
            VStack {
               Text("Hello, World!")
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

检查一下:

struct DoctorHomePage: View {

    @Binding var shouldPopToRootView : Bool

    init(shouldPopToRootView: Binding<Bool>) {

        self._shouldPopToRootView = shouldPopToRootView
        UINavigationBar.appearance().backgroundColor = .clear
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    } // I get the error here

    var body: some View {
        NavigationView {
            VStack {
                Text("Hello, World!")
            }
        }
    }
}

struct ContentView: View {
    var body: some View {
        DoctorHomePage(shouldPopToRootView: .constant(true))
    }
}