Button to navigate through SwiftUI Views

this is my code I need my toolBar buttons to navigate to other SwiftUi views

what should I write in button action { } to open the other SwiftUi View


//  SwiftLoginView.swift

//  Login FireBase

//

//  Created by Makwin Santhosh K on 08/11/22.

//



import SwiftUI

import MapKit

import CoreLocationUI



struct OverallView: View {



    var body: some View{

        

        NavigationView {

            ZStack{

                



                ToolBarShape()

                    .frame(width: 400,height: 110)

                    .foregroundColor(.white)

                    .shadow(radius: 6)

                    .offset(x : 0, y: 410)

                

              

                    .toolbar {

                        ToolbarItemGroup(placement: .bottomBar){

                            //MARK: Button Home

                            Button(action :{

                                

                            }, label:{

                                VStack {

                                    Image(systemName: "house")

                                    Text("Home")

                                }

                                .font(.footnote)

                                .foregroundColor(.black)

                                

                            })

                            Spacer()

                            //MARK: Button Money

                            Button(action :{

                              

                            },label:{

                                VStack {

                                    Image(systemName: "dollarsign")

                                    Text("Money")

                                }

                                .font(.footnote)

                                .foregroundColor(.black)

                                

                            })

                            Spacer()

                            //MARK: Button Home

                            Button(action :{

                                

                            },label:{

                                VStack {

                                    Image(systemName: "person")

                                    Text("Help")

                                }

                                .font(.footnote)

                                .foregroundColor(.black)

                                

                                

                            })

                            //MARK: Button Home

                            Spacer()

                            Button(action :{

                                

                            },label:{

                                VStack {

                                    Image(systemName: "menubar.rectangle")

                                    Text("More")

                                }

                                .font(.footnote)

                                .foregroundColor(.black)

                                

                                

                            })

                            

                            

                        }

                        

                    }

            }

        }

    }

}





    



struct MapUView: View {

    @StateObject public var ViewModel = ContentViewModal()

    

    var body: some View {

        ZStack(alignment: .bottom) {

            AreaMap(region: $ViewModel.region)

            

            LocationButton(.currentLocation){

                ViewModel.requestUserLocationForOnce()

            }

            .foregroundColor(.white)

            .cornerRadius(8)

    



        }

    }

    

    

    

    

    

    

    struct AreaMap: View {

        @Binding var region: MKCoordinateRegion

        

        var body: some View {

            let binding = Binding(

                get: { self.region },

                set: { newValue in

                    DispatchQueue.main.async {

                        self.region = newValue

                    }

                }

            )

            return Map(coordinateRegion: binding, showsUserLocation: true)

                .ignoresSafeArea()

        }

    }

    

    

    final class ContenViewModal: NSObject, ObservableObject, CLLocationManagerDelegate{

        @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 20, longitude: 90), span: MKCoordinateSpan(latitudeDelta: 100, longitudeDelta: 100))

        let locationManager = CLLocationManager()

        

        override init() {

            super.init()

            locationManager.delegate = self

        }

        

        func requestUserLocationForOnce() {

            locationManager.requestLocation()

        }

        

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            guard let latestLocation = locations.first else {

                //show error

                return

            }

            self.region = MKCoordinateRegion(center: latestLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))

        }

        

        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

            print(error.localizedDescription)

        }

    }

}

struct OverallView_Previews: PreviewProvider {

    static var previews: some View {

        OverallView()

    }

}
Answered by darkpaw in 736028022

Well, this is different from what you actually asked us for. You said: "I need my toolBar buttons to navigate to other SwiftUi views", which I showed you how to do. What you actually mean is you want to change the current view.

You could have your toolbar view showing on a MainView(), and the contents of the MainView() is changed depending on which button you've pressed, but the toolbar view remains on top. Something like:

struct MainView: View {
  var body: some View {
    ZStack {
      ShowView(viewId: 1)
      ToolbarView(selectedViewId: 1)
    }
  }
}

struct ShowView(): View {
  var viewId: Int
  var body: some View {
    if(viewId == 1) {
      // HomeView()

    } else if(viewId == 2) {
      ...
    }
  }
}

You could use a NavigationLink instead of a button:

NavigationLink(destination: MapUView()) {
	VStack {
		Image(systemName: "house")
		Text("Home")
	}
	.font(.footnote)
	.foregroundColor(.black)
}

This will show your MapUView when you click it, and it'll add a < Back button in the top left to go back.

You could use a variable that decides which view should be visible.

if visibleView == 1 {
    MapUView
} 

… and than just change the variable with the button.But i think NavigationLinks are better…

Yeah its correct since i have used toolbar items i dont want take the other view to take the whole phone screen space

also i dont want back button i just need to show the other view and simultaneously highlight the toolbar button

so is there any possible way to site other view through button ….

Accepted Answer

Well, this is different from what you actually asked us for. You said: "I need my toolBar buttons to navigate to other SwiftUi views", which I showed you how to do. What you actually mean is you want to change the current view.

You could have your toolbar view showing on a MainView(), and the contents of the MainView() is changed depending on which button you've pressed, but the toolbar view remains on top. Something like:

struct MainView: View {
  var body: some View {
    ZStack {
      ShowView(viewId: 1)
      ToolbarView(selectedViewId: 1)
    }
  }
}

struct ShowView(): View {
  var viewId: Int
  var body: some View {
    if(viewId == 1) {
      // HomeView()

    } else if(viewId == 2) {
      ...
    }
  }
}

Whoah Thanks 🤩

Button to navigate through SwiftUI Views
 
 
Q