【问题标题】:How to Access an About view via Navigation Bar Button如何通过导航栏按钮访问关于视图
【发布时间】:2020-09-30 01:37:26
【问题描述】:

如何将“关于”导航栏按钮链接到“关于”视图?我看过很多关于如何向导航栏添加按钮的帖子,但没有关于如何处理视图链接的帖子。

struct ContentView: View {
     
    var body: some View {
        
        NavigationView {
            VStack {
                NavigationLink(destination: m1View()) {
                    Text("Menu 1")
                }
                .padding()
                
                NavigationLink(destination: m2View()) {
                    Text("Menu 2")
                }
                .padding()
                
                NavigationLink(destination: m3View()) {
                    Text("Menu 3")
                }
                .padding()
            }
            .font(.title)
            .navigationBarTitle("Main View", displayMode: .inline)
            
            .navigationBarItems(trailing: Button(action: {
                     
                    // TODO: add about link here
            }) {
                Text("About")
            }
            )
            .onAppear(perform: loadDataViaBankApi)
        }
    }

        

     func loadDataViaBankApi() -> () {

             ...

     }
}

【问题讨论】:

  • 这取决于你想如何显示关于视图:模态、导航等?
  • 导航到 aboutView 并返回

标签: swiftui swiftui-navigationlink


【解决方案1】:

这是可能的解决方案 - 在导航栏中仅激活链接,但将链接放置在导航视图中

struct ContentView: View {
    @State private var activateAbout = false

    var body: some View {
        
        NavigationView {
            VStack {

              // ... other content here

            }
            .font(.title)
            .navigationBarTitle("Main View", displayMode: .inline)
            .background(
               // hide programmatically activated link here !!
               NavigationLink(destination: AboutView(), isActive: $activateAbout) { EmptyView() }
            )            
            .navigationBarItems(trailing: Button(action: {
               // not add - only activate it here, otherwise it will not work      
               self.activateAbout = true
            }) {
                Text("About")
            }
            )

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多