我可以帮您翻译,以下是翻译结果:
我有两个可重复使用的视图,Ex1和Ex2。我试图根据条件交替显示它们中的一个,但我无法做到。
ContentView:
struct ContentView: View {
@State var selector = false
var cvc = ContentViewController()
var body: some View {
ZStack { // ERROR: Protocol type 'Any' cannot conform to 'View' because only concrete types can conform to protocols
cvc.getView(t: selector)
Button(action: {
self.selector.toggle()
print(self.selector)
}) {
Text("Button")
}
}
}
}
例1:
import SwiftUI
struct Ex1: View {
var body: some View {
Text("Ex 1")
}
}
例 2:
import SwiftUI
struct Ex2: View {
var body: some View {
Text("Ex 2")
}
}
ContentViewController :
import Foundation
class ContentViewController {
let a = Ex1()
let b = Ex2()
func getView (t: Bool) ->(Any){
if t {
return a
}
else {
return b
}
}
}
我认为这很简单,但对于我来说现在不是。请帮我两件事。
我想了解这个问题和解决方案。
在布局中交替显示两个视图的最佳方法。
谢谢提前。