1

我正在尝试导航到一个名为 HomePageView 的新 SwiftUI 文件(当前仅包含红色背景和显示主页的文本。)下面的代码我尝试与我的 Button 集成,这是我最初的 3 个按钮中的 1 个视图是 ContentView。没有错误,但是当我运行我的登录按钮时,它显示“登录已点击!” 文本,但不会将我带到 HomePageView。我是否错误地使用 NavigationLink?我知道我将遇到的下一个问题是一个页面上有多个按钮导致不同的目的地,有什么简单的方法可以解决这个问题,我正在尝试标记方法?

注意:一些视图文本中还有其他代码,它们只是图像和文本字段,以及另外两个按钮

@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))
}
4

2 回答 2

2

如果我在下面的代码中的想法也与您的想法有关,请纠正我。

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


            }
        }
    }

如果像上面那样,那就纠正发生在你身上的事情,因为它只是识别按钮的动作。解决这个只是删除按钮,只在 NavigationLink 中放置文本,如下所示:

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)
            }
        }
    }
于 2019-11-28T09:56:37.487 回答
0

在我的项目中,我使用了另一种解决方案。我不知道这是否也适合你:

A 创建了一个母亲视图

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

您还可以使用动画withAnimation()(请参阅https://developer.apple.com/documentation/swiftui/3279151-withanimation

如果你想看看我的实现,你可以在这里看到: https ://github.com/tristanratz/ChatApp/blob/master/ClientApp/MainView.swift

于 2019-11-28T10:01:19.910 回答