SwiftUI通过按按钮导航到新视图

时间:2019-11-28 00:08:22

标签: button navigation swiftui navigationlink

我正在尝试导航到一个名为HomePageView的新SwiftUI文件(当前仅由红色背景和表示主页的文本组成。)下面的代码试图与我的Button集成,这是3个按钮之一在我的初始视图是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))
}

2 个答案:

答案 0 :(得分: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)
            }
        }
    }

答案 1 :(得分:1)

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

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

相关问题