How to Navigate to a New View from Navigationbar Button Click in Swiftui

How To Switch To A New View On Simple Button Click?

The best way would be to use a NavigationLink and wrapping the content inside a NavigationView.

The NavigationView is used to represent the view hierarchy.

For instance:

// Wrapper for LogIn
struct ContentView: View {
var body: some View {
NavigationView { LogIn() }
}
}

// LogIn
struct LogIn: View {
var body: some View {
VStack {
// Fields for log in

NavigationLink(destination: SignUp()) {
Text("Create New Account")
.foregroundColor(.white)
.font(.title)
.frame(maxWidth: .infinity, alignment: .center)
.background(Color(red: 0.22, green: 0.655, blue: 0.02))
.cornerRadius(8)
.padding(.horizontal, metrics.size.width*0.10)
}
}
}
}

You can find more info in the official docs:

  • NavigationView
  • NavigationLink

Also [Hacking With Swift] (https://www.hackingwithswift.com/quick-start/swiftui/displaying-a-detail-screen-with-navigationlink) is a great resource for SwiftUI

Button, how to open a new View in swiftUI embedded in navigation bar

You just need to add a sheet modifier to your view, which presents your view depending on the value of isAddPresented, just like this:

struct ContentView: View {

@State var isAddPresented = false

var body: some View {
NavigationView {
List(dm.storage){ data in
StileCella(dm2: data)
}
.navigationBarTitle("Lista Rubrica")
.navigationBarItems(trailing: Button("Button") {
self.isAddPresented = true
})
} .sheet(isPresented: $isAddPresented,
onDismiss: { self.isAddPresented = false }) {
DetailView()
}
}
}

The important bit is to remember to set isAddPresented back to false in on dismiss to prevent it form presenting again.

SwiftUI: How to Navigate from one view to another by clicking on a button

To use a NavigationLink you have to wrap it in a NavigationView

struct ContentView: View {
var body: some View {
NavigationView { // <--- add this
NavigationLink(destination: DetailsView()) {
Text("ADD TO CART")
.font(.headline)
.bold()
.padding(.all, 10)
.padding(.leading, 10)
.padding(.trailing, 10)
.foregroundColor(.white)
.background(Color.orange.frame(width: 300, height: 50) .clipShape(RoundedRectangle(cornerRadius: 20)))

}
}
}
}

How to navigate to a View in SwiftUI without NavigationLink

First of all - congratulations: This is the first such complex code, that flawlessly runs on copy/paste into Xcode ... doesn't happen often :)

To your question:

You almost have it. The only thing is that your productFamilyIsActive is a Bool – that can only hold info on ONE selected NavigationLink, you have 3. But it can if you change it to an optional selection:

class Navigation: ObservableObject {
@Published var productFamilyIsActive : String? = nil // here
}

For that you use a different init on NavigationLink here:

struct ProductFamilyRow: View {

@EnvironmentObject var navigation : Navigation
@State var productFamily : String

var body: some View {
NavigationLink(destination: PartNumberView(productFamily: productFamily),
tag: productFamily, // here
selection: $navigation.productFamilyIsActive) { // and here
Text("\(productFamily)")
}
.isDetailLink(false)
}
}

Lastly you don't set to false but to nil here:

struct HomeButtonView: View {

@EnvironmentObject var navigation : Navigation

var body: some View {
Button(action: {
self.navigation.productFamilyIsActive = nil // here
}, label: {
Image(systemName: "house")
})
.environmentObject(navigation)
}
}

Voila!

Go to a new view using SwiftUI

The key is to use a NavigationView and a NavigationLink:

import SwiftUI

struct ContentView : View {
var body: some View {
NavigationView {
VStack {
Text("Hello World")
NavigationLink(destination: DetailView()) {
Text("Do Something")
}
}
}
}
}


Related Topics



Leave a reply



Submit