0

How to initialize shouldPopToRootView? Here is my code:

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 Answer 1

4

check this out:

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))
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Chris: Can you explain the necessity of that underscore thing in front of the variable name? So why it is "self._myVar" and not just "self.myVar"? I lost almost 2 hours to find that underscore trick, wenn I encountered multiple init-problems with a binding in a custom initializer for a struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.