How to go to another view with button click

Jens Thomsen picture Jens Thomsen · Aug 1, 2019 · Viewed 10.8k times · Source

I have a button in my code and I have a file called LogindView.swift

I cannot get the code to open another view file when clicking on the button.

Can anybody give me an example on how to do it.

In my button action I have tried to write LogindView() but i just gives me a warning. "Result of 'LogindView' initializer is unused"

    Button(action: {
            // Do action
            LogindView()
        }, label: {
            //** Label text
            Text("Logind")
                .font(.headline)
                .padding(.all)
                .foregroundColor(Color.white)
        })
        .background(Color.blue)

Answer

Zain picture Zain · Aug 2, 2019

You essentially have 3 options to transition between views depending on your needs.


First, you can use a NavigationView. This will provide a back button and will allow the user to go back. Note that there are some bugs currently when you don't put the NavigationLink inside of a List as per https://stackoverflow.com/a/57122621/3179416

import SwiftUI

struct MasterView: View {
        var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: LoginView()) {
                    Text("Login")
                }
            }
            .navigationBarTitle(Text("Master"))
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

NavigationView


Second, you can present a modal using .sheet. This will present a modal that appears on top of the current view but it can be dismissed by the user by dragging it down.

import SwiftUI

struct MasterView: View {
    @State var isModal: Bool = false

    var body: some View {
        Button("Login") {
            self.isModal = true
        }.sheet(isPresented: $isModal, content: {
            LoginView()
        })
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

.sheet


Third, you can just use an if statement to change the current view to your Login View like so

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
            } else {
                Button("Login") {
                    self.showLoginView = true
                }
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

Changing the view without animation

If you would like to animate this, so that the transition doesn't appear so abruptly, you can also do this:

import SwiftUI

struct MasterView: View {
    @State var showLoginView: Bool = false

    var body: some View {
        VStack {
            if showLoginView {
                LoginView()
                    .animation(.spring())
                    .transition(.slide)
            } else {
                Button("Login") {
                    withAnimation {
                        self.showLoginView = true
                    }
                }.animation(.none)
            }
        }
    }
}

struct LoginView: View {
    var body: some View {
        Text("Login View")
    }
}

Transition with animation