1
votes

I am getting some strange errors at the navigation link to aboutView (bottom of ContentView):

  1. aboutView' initializer is inaccessible due to 'private' protection level
  2. Missing argument for parameter 'useHomeCur' in call
  3. Expected expression Xcode is also suggesting that I Insert ', useHomeCur: <#Bool#>' in ContentView

I don't get these errors in with any other view. No errors are occurring in aboutView.swift. It acts as if aboutView is a subView of ContentView. It is wanting me to define an aboutView state parameter in ContentView. Why? If I text out the state parameter and option in aboutView things work great.

I have also tried: .navigationBarItems(trailing: NavigationLink( destination: aboutView()) { Text("About"). This coding removes the expected expression error

iOS 14.0 Xcode 12.0.1 Galen

struct ContentView: View {

    @State private var activateAbout = false
    
    var body: some View {
        
        NavigationView {
            VStack {
                
                   ... menu navigation links
            }
            .font(.title)
            .navigationBarTitle("blah blah blah", displayMode: .inline)
            .background(
                // hide programmatically activated link here !!
                NavigationLink(destination: aboutView(), isActive: $activateAbout) { EmptyView()
            )
                    .navigationBarItems(trailing: Button( action: {
                    // not add - only activate it here, otherwise it will not work
                        self.activateAbout = true
                    }) {
                        Text("About")
                    }
                }           
            }
            
            .onAppear(perform: loadCurrencies)
        }
    }
}

Here is the aboutView>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

struct aboutView: View {
    
    @EnvironmentObject var userData: UserData
    @State private var useHomeCur: Bool
    
    var body: some View {

        NavigationView {
        Form {
                VStack (alignment: .center) {
                    Text("blah blah blah")
                    Text("blah blah blah")
                }.font(.body)
            
                Toggle(isOn: $useHomeCur) {
                    Text("Use this fancy feature")
               }.padding()

           NavigationLink(destination: docView()) {
                Text("Documentation")
            }
        }.navigationBarTitle("About", displayMode: .inline)
  
        }
    }
}
1

1 Answers

1
votes

State variable is for internal View usage, so should be initialised internally (and good practice to have it private). If you need the state to have initial value from outsize then do it in init explicitly, like

  1. variant of internal initialization
struct aboutView: View {
    
    @EnvironmentObject var userData: UserData
    @State private var useHomeCur: Bool = false    // << here !!
  1. variant of external initialization
struct aboutView: View {
    
    @EnvironmentObject var userData: UserData
    @State private var useHomeCur: Bool

    init(useHomeCur: Bool = false) {
       _useHomeCur = State(initialValue: useHomeCur)    // << here !!
    }
    ...