How do I go to another view in swiftUI programmatically using Button (action:{}) {}?

余生长醉 提交于 2020-01-06 05:25:14

问题


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.


回答1:


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!




回答2:


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())
            }
        }
    }


来源:https://stackoverflow.com/questions/59292634/how-do-i-go-to-another-view-in-swiftui-programmatically-using-button-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!