繁体   English   中英

为什么 NavigationLink 不能使用 SwiftUI 导航

[英]Why is NavigationLink not navigation with SwiftUI

我正在尝试使用 NavigationLink 导航到 LoginScreen。 然而,点击按钮并没有做任何事情。 如果我调用 onTapGesture 方法,它就会被触发。

struct SplashScreen: View {

    @State
    var shouldGoHome: Bool = false
    @State
    var goToDestination: Bool = false
    @ObservedObject private var viewModel = SplashViewModel()

    var body: some View {
        VStack {
            VColorBackground()
            Text("VOWER")
                .foregroundColor(Color(ColorTheme.brandBlue.value))
                .font(.system(size: 36))
                .tracking(20)
            Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
                .tracking(2)
                .multilineTextAlignment(.center)
                .padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
                .font(.system(size: 14))
                .foregroundColor(Color.black)
            if (viewModel.isUserLoggedIn) {
                VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
                    AppView()
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            } else {
                NavigationLink(destination: LoginScreen()) {
                    VowerButtonStyle(text: "Get Started")
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            }


            Spacer()

        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.all)
    }
}

您必须将视图包装在 NavigationView { } 中,否则 NavigationLink 将无法工作。

struct SplashScreen: View {

@State
var shouldGoHome: Bool = false
@State
var goToDestination: Bool = false
@ObservedObject private var viewModel = SplashViewModel()

var body: some View {
    NavigationView{ 
        VStack {
            VColorBackground()
            Text("VOWER")
                .foregroundColor(Color(ColorTheme.brandBlue.value))
                .font(.system(size: 36))
                .tracking(20)
            Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
                .tracking(2)
                .multilineTextAlignment(.center)
                .padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
                .font(.system(size: 14))
                .foregroundColor(Color.black)
            if (viewModel.isUserLoggedIn) {
                VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
                    AppView()
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            } else {
                NavigationLink(destination: LoginScreen()) {
                    VowerButtonStyle(text: "Get Started")
                }
                .padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
            }


            Spacer()

        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        .edgesIgnoringSafeArea(.all)
    }
}

}

为了更好地理解导航,请看下面的代码

var body: some View {
    NavigationView{
        VStack(){
            Text("VOWER")
            NavigationLink(destination: detailView){
                VStack(){
                    Text("Sunset").foregroundColor(Color.blue)
                }
            }.navigationBarTitle("Login") //define Title bar for better understanding and ease
        }
    }
}

同样在目标视图SwiftUI类中,可以定义navigationBarTitle

    var body: some View {
     VStack(){
        Text("Turn Notification On/Off")
     }.navigationBarTitle("Settings")
    }

- 它在设备上运行良好,模拟器有第二次无法运行的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM