简体   繁体   中英

How do I go to another view in swiftUI programmatically using Button (action:{}) {}, and NOT any NavigationLink or whatever?

I am trying to programmatically change views in my macOS application. I found stuff like NavigationLink and others but I am not allowed to use them. The code to switch must be in Button's action, because this way I know that I can quickly switch from the code, even if it is not in button format. How can I do this?

Button in ContentView.swift:

Button(action: {
    //What goes here. Tried Settings(), didn't work          
}) {
    Text("Settings")
}.buttonStyle(PlainButtonStyle())

Settings.swift:

import SwiftUI

struct Settings: View {
    var body: some View {
        Text("Hello, world!")
    }
}

No NavigationLink, No NavigationView or whatever. I am looking for a way to do this purely within the action of the button.

I have another question about this topic so if you know swiftui click here and it is critical to me to have it answered. If you answered this question maybe you can answer the other one, and if that question gets an acceptable answer, then that'll be lit.

Basically, you need 3 views here to achieve this functionality, Main ContentView to switch views (HomeView & SettingView).

Here HomeView contains Setting button and has one @Binding variable to handle button click on ContentView .

So, whenever you click on Setting button on HomeView it will toggle value of settingView variable of ContentView and switch view according to it's value.

try below code.

ContentView :

struct ContentView: View {
    @State var settingView = false

    var body: some View {
        VStack {
            if self.settingView {
                Settings()
            } else {
              HomeView(buttonClick: $settingView)
            }
        }
    }
}

HomeView :

struct HomeView: View {
        @Binding var buttonClick: Bool

        var body: some View {
            VStack {
                Button(action: {
                    withAnimation {
                        self.buttonClick.toggle()
                    }
                }) {
                    Text("Settings")
                }.buttonStyle(PlainButtonStyle())
            }
        }
    }

Here is an example:

In the action block of the button you can not put a view . But you can toggle a bool , which is a @State variable, which updates the view and will show a different view when true . I added an animation with .transition() , but you need to put withAnimation around the change of the @State variable to make it work.

struct ContentView: View {
    @State var showView = false

    var body: some View {
        GeometryReader { proxy in
            ZStack {
                if self.showView {
                    VStack {
                        Text("Settings View")
                        Button(action: {
                            withAnimation {
                                self.showView.toggle()
                            }
                        }, label: {
                            Text("Back")
                        })
                    }
                    .frame(width: proxy.size.width, height: proxy.size.height)
                    .background(Color.red)
                    .transition(.move(edge: .trailing))
                } else {
                    VStack {
                        Text("Home View")
                        Button(action: {
                            withAnimation {
                                self.showView.toggle()
                            }
                        }, label: {
                            Text("Settings")
                        })
                    }
                    .frame(width: proxy.size.width, height: proxy.size.height)
                    .background(Color.green)
                }
            }
        }
    }
}

I hope this helps!

If you click start on the first view, it navigates you to the next view. You can edit the first view to fit your view requirements

GloryHUbApp.swift

import SwiftUI

@main
struct GloryHubApp: App {
@State var currentStage = "OnboardingView"
var body: some Scene {
    WindowGroup {
            if(self.currentStage == "OnboardingView"){
                StartButtonView(currentStage: $currentStage)
            } else if(self.currentStage == "LoginView"){
                NextView()
            }
    }
  }
}

ButtonView.swift

import SwiftUI
struct StartButtonView: View {
// MARK: - PROPERTIES
@Binding var currentStage: String
// MARK: - BODY
var body: some View {
    Button(action: {
        //appStage = "LoginView"
        self.currentStage = "LoginView"
    }) {
        HStack (spacing: 8) {
            Text("START")
        }
    } //: BUTTO
  }
}
import SwiftUI

struct StartButtonView: View {

    @State private var isActive = false

    var body: some View {

    Button(action: {
       isActive = true
    }, label: { 
       Text("START")
    })

    NavigationLink(destination: Settings(), isActive: $isActive) { }
}
            

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