How to switch SwiftUI Views?

Hello. I am stuck on this one problem, and I can't fix it. I'm very new to Swift, but I have used other languages. I searched all over the internet, and it does not work. I tried doing something similar to this:

Button("Press Me") {
     view2()
}

It doesn't do anything. It's spelled correctly. If it helps, it also gives a warning of "Result of 'Username' initializer is unused" Does anyone know why this is happening?

You want to change SwiftUI Views by pressing the button, right? If so, you can use NavigationLink with NavigationView.

An example:

        NavigationView{

            NavigationLink(destination: {

                view2()

            }, label: {

                Text("Press Me")

                    .foregroundColor(.white)  // Set the modifiers to your liking.

                    .padding(.vertical)

                    .padding(.horizontal, 50)

                    .background(.blue)

                    .cornerRadius(10)

            })

            .navigationTitle("Home")  // You can set the title that displayed at the top of the view.

        }

More information, see Apple Developer Documentation.

However, NavigationView is deprecated in iOS16. It seems that we can use new navigation types in iOS16 or later.

but it still shows the other text from ContentView()

Do you want to hide text like below?

If so, you can hide it by adding .navigationBarHidden(true) in view2() like this:

struct view2: View {

    var body: some View {

        VStack {

            Text("Some Text...")

            Text("Some Text...")

            Text("Some Text...")

        }

        .navigationBarHidden(true)

    }

}

Also, if you want to add a button to return to ContentView(), you can use dismiss() action.

How to use:

struct view2: View {

    @Environment(\.isPresented) var isPresented

    @Environment(\.dismiss) var dismiss

    var body: some View {

        VStack {

            Text("Some Text...")

            Text("Some Text...")

            Text("Some Text...")

            if isPresented {

                Button(action: {

                    dismiss()

                }, label: {

                    Text("Back")

                })

            }

        }

        .navigationBarHidden(true)

    }

}

Hello,

I also devised way to switch Views without NavigationView. It use switch statement.

Sample here:

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var pageManager: PageManager

    var body: some View {
        switch pageManager.page {
        case .top:
            top()        

        case .view2:
            view2()
        }
    }
}

struct top: View {
    @EnvironmentObject var pageManager: PageManager

    var body: some View{
        VStack {
            Text("Top")
                .padding()

            Button("To view2"){
                pageManager.page = .view2
            }
            .padding()
        }
    }
}

struct view2: View {
    @EnvironmentObject var pageManager: PageManager

    var body: some View {
        VStack {
            Text("View2")
                .padding()

            Button("To top"){
                pageManager.page = .top
            }
            .padding()
        }
    }
}

enum Page {
    case top
    case view2
}

class PageManager: ObservableObject{
    @Published var page: Page = .top
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(PageManager())
    }
}

You can try something like this, at least is it working for me. import SwiftUI


enum Page {
    case LoginView
    case MainView
}

class PageManager: ObservableObject{
    @Published var page: Page
    init(page: Page) {
        self.page = page
    }
}


@main
struct RandomApp: App {
    @ObservedObject var pageManager = PageManager(page: .LoginView)

    var body: some Scene {
        WindowGroup {
            switch(pageManager.page){
            case Page.LoginView:
                LoginView()
                    .environmentObject(pageManager)
            case Page.MainView:
                MainView()
                    .environmentObject(pageManager)
            }
        }
    }
}

In any of your views: pageManager.page = .MainView

struct LoginView: View {
    @EnvironmentObject var pageManager: PageManager
...
    func login(username: String, password: String) -> Bool {
        // In here your check to see if username/password are valid
        if username == "x", password == "x" {
            pageManager.page = .MainView
            return true
        }
        return false
    }
}
How to switch SwiftUI Views?
 
 
Q