Reputation: 387
I am getting some strange errors at the navigation link to aboutView (bottom of 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)
}
}
}
Upvotes: 1
Views: 524
Reputation: 257493
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
struct aboutView: View {
@EnvironmentObject var userData: UserData
@State private var useHomeCur: Bool = false // << here !!
struct aboutView: View {
@EnvironmentObject var userData: UserData
@State private var useHomeCur: Bool
init(useHomeCur: Bool = false) {
_useHomeCur = State(initialValue: useHomeCur) // << here !!
}
...
Upvotes: 1