如何将一个SwiftUI View作为变量传递给另一个View结构 [英] How to pass one SwiftUI View as a variable to another View struct
问题描述
我正在实现名为MenuItem
的非常自定义NavigationLink,并希望在整个项目中重复使用它.这是一个符合View
并实现包含NavigationLink
的var body : some View
的结构.
我需要以某种方式将应由NavigationLink
呈现的视图存储在MenuItem
的正文中,但尚未这样做.
I'm implementing a very custom NavigationLink called MenuItem
and would like to reuse it across the project. It's a struct that conforms to View
and implements var body : some View
which contains a NavigationLink
.
I need to somehow store the view that shall be presented by NavigationLink
in the body of MenuItem
but have yet failed to do so.
我在MenuItem
的主体中将destinationView
定义为some View
,并尝试了两个初始化程序:
I have defined destinationView
in MenuItem
's body as some View
and tried two initializers:
这似乎太简单了:
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.
第二次尝试:
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...
}
}
->错误:无法将类型为"V"的值分配为类型为某些视图".
--> Error: Cannot assign value of type 'V' to type 'some View'.
最终尝试:
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可以接受某些View作为参数,则必须有一种方法. 谢谢; D
I hope someone can help me. There must be a way if NavigationLink can accept some View as an argument. Thanks ;D
推荐答案
总结一下我在这里阅读的所有内容以及在 iOS14 上适用于我的解决方案:
To sum up everything I read here and the solution which worked for me and on iOS14:
struct ContainerView<Content: View>: View {
let content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
// Do something with `content`
}
}
这不仅允许您将简单的View
放入其中,而且由于@ViewBuilder
的原因,还可以使用if-else
和switch-case
块:
This not only allows you to put simple View
s inside, but also, thanks to @ViewBuilder
, use if-else
and switch-case
blocks:
struct SimpleView: View {
var body: some View {
ContainerView {
Text("SimpleView Text")
}
}
}
struct IfElseView: View {
var flag = true
var body: some View {
ContainerView {
if flag {
Text("True text")
} else {
Text("False text")
}
}
}
}
struct SwitchCaseView: View {
var condition = 1
var body: some View {
ContainerView {
switch condition {
case 1:
Text("One")
case 2:
Text("Two")
default:
Text("Default")
}
}
}
}
这篇关于如何将一个SwiftUI View作为变量传递给另一个View结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!