I am trying to navigate to a subview and initialize it from a property passed from the parent view WITHOUT using onAppear.
I am able to make this work with onAppear but don't like how it calls the function everytime the view shows up and there has to be a cleaner way??
I want to initialize PopupCollectorView by calling loadCollectors in its init() function but I keep getting errors:
"Variable 'self.profileData' used before being initialized"
"Variable 'self.profileData' used before being initialized"
"Return from initializer without initializing all stored properties"
How can I make the subview view call loadCollectors() with the profileData I am passing to it??
THANK YOU
PARENT VIEW - all inside a navigation view hierarchy...
struct PopupProfileFull: View {
@ObservedObject var popupVM : PopupProfileViewModel
@State var presentCollectors: Bool = false
var body: some View {
VStack {
if let profileData = popupVM.profileData {
// tapping on this triggers navigation to popupCollectorsView
PopupAccountStats(popupVM: popupVM, presentCollectors: $presentCollectors, presentCollecting: $presentCollecting)
.background (
NavigationLink("", destination: PopupCollectorsView(profileData: $popupVM.profileData), isActive: $presentCollectors)
)
}
}
}
}
CHILD VIEW NAVIGATED TO FROM PARENT
struct PopupCollectorsView: View {
@StateObject var popupCollectorVM = PopupCollectorsViewModel()
@Binding var profileData: ProfileData?
// ERRORS OCCUR HERE
init(profileData: ProfileData) {
self.profileData = profileData
loadCollectors()
}
var body: some View {
VStack {
List(0..<popupCollectorVM.collectors.count, id: \.self) { i in
Text(popupCollectorVM.collectors[i].collectedUsername)
}
}
}
func loadCollectors() {
if let profileData = profileData {
popupCollectorVM.loadCollectors(userId: profileData.userId)
}
}
}