SwiftUI NavigationLink 如何访问另一个 SwiftUI 页面?



我正在尝试从一个 SwiftUI 视图转到另一个 SwiftUI 视图,并且我正在按照下面的代码使用导航链接,但我收到错误:无法调用类型"NavigationLink<_、_>"的初始值设定项,参数列表类型为"(目的地:播放列表表(">

下面是我的按钮代码,该按钮触发了指向下一个视图的链接:

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

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

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

谢尔盖,背景仍然是最好的解决方案? 尝试在 Firebase 用户身份验证和用户成功创建后导航

最新更新