My ContentView holds a TabView with 3 tabItems, but there's 4 views total. I get to the 4th view using a navigationLink that wraps around list rows.
My problem is that when I press the navigationLink I get a weird nested view as I've shown below:
I feel like I need to navigate to a different tabItem by pressing the list row but I don't know if that's possible. Help would be much appreciated since I'm new to using SwiftUI...
This is my code:
struct SavedPlacesView: View {
@EnvironmentObject var viewModel: WeatherData
@State private var showPopup = false
@State private var userInput = ""
@State private var newPlace: SavedPlacesStruct? = nil
var body: some View {
NavigationView {
VStack {
//list populates using the struct to define the UI and the array to hold the data. pass to data to struct to fill list.
List{
ForEach(SavedPlacesArray, id: \.id) { Place in
SavedPlaceCellView(savedPlaces: Place)
}
.onDelete(perform: deleteLocation)
}
Spacer()
ZStack {
Button("Save current location") {
showPopup = true
}
.alert("Name your location", isPresented: $showPopup, actions: {
TextField("Location Name", text: $userInput)
Button("Save", action: {
let newSavedPlace = SavedPlacesStruct(savedLatitude: selectedCords.latitude, savedLongitude: selectedCords.longitude, savedName: userInput)
SavedPlacesArray.append(newSavedPlace)
viewModel.savePlacesToUserDefaults()
})
Button("Cancel", role: .cancel, action: {})
})
.frame(width: 200, height: 50 )
.background(.clear)
.foregroundColor(Color(.link))
.cornerRadius(15)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.link),lineWidth: 2))
.padding(.horizontal)
.padding(.bottom, 50)
.onChange(of: showPopup) { value in
if value == false {
// Reset the user input when the popup is dismissed
userInput = ""
}
}
}
.frame(maxWidth: .infinity, maxHeight: 50, alignment: .bottom)
}
.navigationTitle("Saved Places")
}
}
//Function that deletes the location from the userDefaults
func deleteLocation(at offsets: IndexSet) {
SavedPlacesArray.remove(atOffsets: offsets)
viewModel.savePlacesToUserDefaults()
}
}
struct SavedPlaceCellView: View {
let savedPlaces: SavedPlacesStruct
@EnvironmentObject var viewModel: WeatherData
var body: some View{
HStack {
NavigationLink(
destination: ModelSelectView()
.onAppear {
selectedCords.latitude = savedPlaces.savedLatitude
selectedCords.longitude = savedPlaces.savedLongitude
fetchModelInventory(for: viewModel)
},
label: {
Text(savedPlaces.savedName)
})
}
}
}
And this is my code for the ContentView:
struct ContentView: View {
@EnvironmentObject var viewModel: WeatherData
var body: some View {
TabView {
MapView()
.tabItem{
Image(systemName: "map")
Text("Location")
}
.edgesIgnoringSafeArea(.top)
SavedPlacesView()
.tabItem{
Image(systemName: "star")
Text("Favourites")
}
ModelSelectView()
.tabItem{
Image(systemName: "filemenu.and.selection")
Text("Model")
}
}
}
}