SwiftUI NavigationLink 如何到达另一个 SwiftUI 页面?

SwiftUI NavigationLink how to get to another SwiftUI page?

我正在尝试从一个 SwiftUI 视图转到另一个 SwiftUI 视图,并且我正在按照下面的代码使用 NavigationLink,但出现错误:无法为类型 'NavigationLink<_, _>' 调用初始值设定项参数列表类型为“(destination: PlaylistTable)”

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

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))
            }
        }
    }
}

不要将 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))
        }
    }

如果要使用按钮,请将导航 link 移至后台。

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()}
         )
    }
}