简体   繁体   中英

SwiftUI navigate to a new view by pressing Button

I am trying to navigate to a new SwiftUI file that I called HomePageView (Currently just consist of a red background and a text that says Home page.) The code below I tried to integrate with my Button which is 1 of 3 buttons on my initial view which is the ContentView. There are no errors but when I run my code my Login button, it shows the "Login Tapped!" text, but does not take me to the HomePageView. Am I using NavigationLink incorrectly? I know the next problem I will run into is with multiple buttons on one page leading to different destinations, any easy way to solve this, I am trying the tag method?

Note: There is other code in the some View text that are just images and textfields, as well as the two other buttons

@State private var current: Int? = nil

var body: some View {
    NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
        EmptyView()
    }

    Button(action: {
        self.current = 1
        print("Login tapped!")

    }) {
        Text("Login")
            .fontWeight(.bold)
            .foregroundColor(.orange)
            .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
            .cornerRadius(50)
            .overlay(
                Capsule(style: .continuous)
                    .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
            .frame(width: deviceSize.size.width, alignment: .center)

    }.offset(y: deviceSize.size.height*(560/812))
}

correct me if my thinking as code below is also about your idea.

var body: some View {
        NavigationView {
            NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
                Button(action: {
                    print("AAAA")
                }) {
                    Text("Login")
                    .fontWeight(.bold)
                    .foregroundColor(.orange)
                    .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
                    .cornerRadius(50)
                    .overlay(
                        Capsule(style: .continuous)
                            .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
                    .frame(width: deviceSize.size.width, alignment: .center)
                }


            }
        }
    }

If like as above, that correct what happening to you because it just recognized action of button. Resolve this just remove button, only put text in NavigationLink as below:

var body: some View {
        NavigationView {
            NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
                Text("Login")
                .fontWeight(.bold)
                .foregroundColor(.orange)
                .frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
                .cornerRadius(50)
                .overlay(
                    Capsule(style: .continuous)
                        .stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
                .frame(width: deviceSize.size.width, alignment: .center)
            }
        }
    }

In my project I used another solution. I don't know if this maybe works for you too:

A created a mother view with

if current == 0 {
    CurrentView()
} else {
    HomePageView()
}

You can also animate it with withAnimation() (see https://developer.apple.com/documentation/swiftui/3279151-withanimation )

If you want to take a look at my implementation you can see it here: https://github.com/tristanratz/ChatApp/blob/master/ClientApp/MainView.swift

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