我正在实现一个名为MenuItem
的very定制NavigationLink,并希望在整个项目中重用它.它是一个符合View
的 struct ,实现了包含NavigationLink
的var body : some View
.
我在MenuItem
的正文中将destinationView
定义为some View
,并try 了两个初始值设定项:
这似乎太容易了:
struct MenuItem: View {
private var destinationView: some View
init(destinationView: View) {
self.destinationView = destinationView
}
var body : some View {
// Here I'm passing destinationView to NavigationLink...
}
}
--> Error: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements.
第二次try :
struct MenuItem: View {
private var destinationView: some View
init<V>(destinationView: V) where V: View {
self.destinationView = destinationView
}
var body : some View {
// Here I'm passing destinationView to NavigationLink...
}
}
--> Error: Cannot assign value of type 'V' to type 'some View'.
最后一次try :
struct MenuItem: View {
private var destinationView: some View
init<V>(destinationView: V) where V: View {
self.destinationView = destinationView as View
}
var body : some View {
// Here I'm passing destinationView to NavigationLink...
}
}
--> Error: Cannot assign value of type 'View' to type 'some View'.
我希望有人能帮助我.如果NavigationLink可以接受某些视图作为参数,那么肯定有办法.