如何在 SwiftUI 中为 NavigationLink 创建工厂方法?
How to create a factory method for NavigationLink in SwiftUI?
我想为 NavigationLink:s 创建一个工厂方法,如下所示:
func makeNavigationLink(label: String, destination: View) -> some View {
NavigationLink(destination: StatsView(), label: {
Text(label)
.foregroundColor(.white)
.font(.title)
})
}
这会产生错误:Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
这应该如何编码?
添加视图约束。
func makeNavigationLink<Destination: View>(label: String, destination: Destination) -> some View {
NavigationLink(destination: StatsView(), label: {
Text(label)
.foregroundColor(.white)
.font(.title)
})
}
我想为 NavigationLink:s 创建一个工厂方法,如下所示:
func makeNavigationLink(label: String, destination: View) -> some View {
NavigationLink(destination: StatsView(), label: {
Text(label)
.foregroundColor(.white)
.font(.title)
})
}
这会产生错误:Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements
这应该如何编码?
添加视图约束。
func makeNavigationLink<Destination: View>(label: String, destination: Destination) -> some View {
NavigationLink(destination: StatsView(), label: {
Text(label)
.foregroundColor(.white)
.font(.title)
})
}