简体   繁体   中英

Swift go to a view by clicking on a button without using StoryBoard

Please how we can go to a view by clicking on a button without using StoryBoard or controller . I only use View

If the user clicks on connexionBtnView I want to redirect them to an AdminView or UserView


import SwiftUI

struct ConnexionView: View {
    
    @State var loginId: String  = ""
    @State var pwd: String  = ""
    @StateObject private var keyboardHander = KeyBoardHandler()
    
    var body: some View {
        
        NavigationView{
            
            ZStack{
                
                Image("background")
                    .ignoresSafeArea()
                
                VStack (spacing: 15){
                    
                    Spacer()
                    logoView
                    Spacer()
                    titleView
                    loginIdView
                    loginPwdView
                    connexionBtnView 
                    Spacer()
                    NavigationLink {
                        LostPwdView()
                    } label: {
                        lostPwd
                    }
                    Spacer()
                }.frame(maxHeight: .infinity)
                    .padding(.bottom,keyboardHander.keyboardHeight)
                    .animation(.default)
                
                
            }
            
        }
        
    }

Thnaks

The NavigationLink has the isActive parameter. You can pass it in the init of NavigationLink and when this state variable has the true value you will redirect to another view. Details here .

struct ConnexionView: View {
    
    @State var isActive: Bool = false
    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(isActive: $isActive) {
                    LostPwdView()
                } label: {
                    Text("Some Label")
                }
                Button("Tap me!") {
                    isActive = true
                }
            }
        }
    }
}

struct LostPwdView: View {
    var body: some View {
        Text("Hello")
    }
}

What you need to do is having a @State variable that would trigger the navigation:

.fullScreenCover(
    isPresented: $viewShown
) {
    print("View dismissed")
} content: {
    NextView()
}

Where NextView() is the View you want to show and the viewShown is your State variable, below a full example:

struct ExampleView: View {
    
    @State var isNextPageOpen = false
    
    var body: some View {
         Button("Tap Here To Navigate") {
             isNextPageOpen = true
         }
         .fullScreenCover(
             isPresented: $isNextPageOpen
         ) {
             print("View dismissed")
         } content: {
             NextView()
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM