【问题标题】:SwiftUI NavigationLink how to get to another SwiftUI page?SwiftUI NavigationLink 如何到达另一个 SwiftUI 页面?
【发布时间】:2020-03-31 20:58:10
【问题描述】:

我正在尝试从一个 SwiftUI 视图转到另一个 SwiftUI 视图,并且我正在按照下面的代码使用 NavigationLink,但我收到了错误:无法调用类型为 'NavigationLink<_ _> 的初始化程序' 带有类型为 '(destination: PlaylistTable)' 的参数列表

下面是触发链接到下一个视图的按钮的代码:

struct MusicButton: View {
    var body: some View {
        NavigationView {
        Button(action: {
            NavigationLink(destination: PlaylistTable())
        })
        { Image(systemName: "music.note.list")
            .resizable()
            .foregroundColor(Color.white)
            .frame(width: 25, height: 25, alignment: .center)
            .aspectRatio(contentMode: .fit)
            .font(Font.title.weight(.ultraLight))
            }
        }
    }
}

【问题讨论】:

    标签: swiftui swiftui-navigationlink


    【解决方案1】:

    不要将NavigationLink 放在Button 中会解决您的问题:

     NavigationView {
    
            NavigationLink(destination: PlaylistTable())
        { Image(systemName: "music.note.list")
            .resizable()
            .foregroundColor(Color.white)
            .frame(width: 25, height: 25, alignment: .center)
            .aspectRatio(contentMode: .fit)
            .font(Font.title.weight(.ultraLight))
            }
        }
    

    【讨论】:

      【解决方案2】:

      如果要使用按钮,请将导航链接移至背景。

      struct MusicButton: View {
      
          @State var isActive = false
      
          var body: some View {
              NavigationView {
              Button(action: {
                  isActive.toggle()
              })
              { Image(systemName: "music.note.list")
                  .resizable()
                  .foregroundColor(Color.white)
                  .frame(width: 25, height: 25, alignment: .center)
                  .aspectRatio(contentMode: .fit)
                  .font(Font.title.weight(.ultraLight))
                  }
              }
              .background(
                   NavigationLink(destination: PlaylistTable(), isActive: $isActive) {EmptyView()}
               )
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-04
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2022-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多