【问题标题】:I can't click and navigate to another page using Button() and NavigationLink()我无法使用 Button() 和 NavigationLink() 单击并导航到另一个页面
【发布时间】:2022-08-15 03:33:52
【问题描述】:

???一旦触发 \"Logout\" 文本或 \"logout\" 图标,我就有以下代码段导航到另一个页面:

         //$tempSignout is a boolean var -> @State  var tempSignout = false

        NavigationLink(destination:   SigninView(), isActive: $tempSignout) {EmptyView()}
       
        Button(action: {
            tempSignout = true
        }, label: {
            HStack{
                Text(\"Logout\")
                    .underline()
                    .foregroundColor(Color(\"logoutColor\"))
                    .frame(width: 100, height: 50)
                    .padding(.trailing, -100)
                
                
                Image(\"logout\").resizable()
                    .frame(width: 30, height: 30)
                    .padding(.leading, 100)
            }
            
            
        })

???这是代表上述代码段的 UI:

???问题是:无论我按注销文本还是图标,我都无法导航到目标页面,它似乎根本无法点击。

标签: swift swiftui swiftui-navigationlink


【解决方案1】:

尝试将所有内容包装在一个导航视图因为没有 NavigationView,导航不起作用。

NavigationView {
//all your contents, stacks, buttons, etc
}

【讨论】:

    【解决方案2】:

    为了使其正常工作,您需要将整个视图主体包装在导航视图中。例如。

    struct ExampleView: View {
        struct BananasView: View {
            var body: some View {
                Text("Bananas")
                    .navigationTitle("??")
            }
        }
    
        var body: some View {
            NavigationView {
                NavigationLink(destination: BananasView()) {
                    Text("I want bananas!")
                }
            }
        }
    }
    

    来自https://swiftontap.com/navigationlink 的示例。了解这一点的好资源是Navigation Link Tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多