Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to another view with button click

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)
like image 989
Jens Thomsen Avatar asked Aug 01 '19 15:08

Jens Thomsen


People also ask

How do I navigate to a different view in SwiftUI?

Getting Started with Navigation Links:Text("Navigate here.") Text("Now on the second view.") As you can see from the example above, the navigation link will act as a standard link. Once clicked, the application will then navigate to and display the view that was set as the destination.

How do I navigate in SwiftUI?

SwiftUI's NavigationLink has a second initializer that has an isActive parameter, allowing us to read or write whether the navigation link is currently active. In practical terms, this means we can programmatically trigger the activation of a navigation link by setting whatever state it's watching to true.

How do I redirect to another page when clicking a button?

How do I redirect to another page when clicking a button in Laravel. My next page name is “next”. which we are going to link the page through the button. Step1:- Where you are creating the button, put the link in it. Step2:- Then go to web.php (inside routes) and put some commands. Route::get ('/create', 'HomeController@create')- > name ('create');

How to open the second screen using onclicklistener in JavaScript?

First, create two activities and add a button to first activity as shown in the following image. In this case, my button's id is button Now open the code for the first screen and access the button using id. Add an OnClickListener to the button and write following code for opening the second screen in OnClickListener.

How to redirect to another view or page using jQuery?

By using jQuery window.location.href property we can easily redirect to another view or page or controller action method based on our requirements.

How do I create a button with a link in it?

Step1:- Where you are creating the button, put the link in it. Step2:- Then go to web.php (inside routes) and put some commands. Route::get ('/create', 'HomeController@create')- > name ('create'); Step3:- Then go to home controller.php and write after the index function


2 Answers

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

like image 94
Zain Avatar answered Oct 19 '22 20:10

Zain


You can use navigation link instead button

var body: some View {
    VStack {
        Text("Title")
            .font(.headline)
        Image("myimage").clipShape(Circle())
        Text("mytext").font(.title)
        
        NavigationLink(destination: AnotherView()) {
           Image(systemName: "person.circle").imageScale(.large)
        }
  
    }
}
like image 4
fiona16ti Avatar answered Oct 19 '22 20:10

fiona16ti