代码之家  ›  专栏  ›  技术社区  ›  Adrian Le Roy Devezin

为什么NavigationLink不能使用SwiftUI导航

  •  0
  • Adrian Le Roy Devezin  · 技术社区  · 4 年前

    我正在尝试使用导航链接导航到登录屏幕。然而,点击按钮并没有任何作用。如果我调用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)
        }
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Mahmud Ahsan    4 年前

    您必须将视图包裹在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)
        }
    }
    

    }

        2
  •  1
  •   Komal Gupta    4 年前

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

    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 Class中,您可以定义 navigationBarTitle

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

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